我正在尝试使用requirejs文本插件在Cordova项目中加载文本资源:
var template = require( "text!app/assets/templates/stuff.html" );
但是,我只得到一个空字符串。我可以看到任何地方都没有错误。有什么想法吗?
没关系,弄清楚了。对于可能遇到这个问题的其他人...
XHR限制意味着文本插件在Cordova内部可能是奇怪的。
解决方案是需要一个返回文本字符串的JavaScript模块。因此,例如
define(function(require) {
var tmpl = `
<div>test</div>
Ok here is a test
`;
return tmpl;
});
,然后...
var template = require( "app/assets/templates/stuff.html" );
shazam!(而且由于背键允许在ECMA6中进行多行字符串,因此它仍然非常可读。)