我应该如何使用通过bower安装的requirejs-text?我应该把它放在baseUrl中,但不知道我是否可以从components/requirejs-text/中使用它?最佳实践是什么?
发布于 2013-06-10 23:27:53
在配置中定义插件的路径:
requirejs.config({
paths: {
"text" : "components/requirejs-text/text"
}
},并按照https://github.com/requirejs/text上的文档在您的模块中使用它:
require(["some/module", "text!some/module.html", "text!some/module.css"],
function(module, html, css) {
//the html variable will be the text
//of the some/module.html file
//the css variable will be the text
//of the some/module.css file.
}
);从技术上讲,您也可以在requirejs.config中使用没有路径定义的插件,但这可能不是最佳实践:
require(["your_path_to_the_plugin_from_baseurl/without_js_at_the_end!some/textfile"],
function(yourTextfile) {
}
);发布于 2013-10-08 00:28:31
在PROJECT_APP/bower.js中,在dependencies部分下添加以下行:
"requirejs": "~2.1.8",
"requirejs-text":"~2.0.10", // this is new
"qunit": "~1.12.0",然后运行bower install,它应该安装这个插件,并在最后显示一个路径,如requirejs-text#2.0.10 vendor/bower/requirejs-text (取决于您的配置)。
最后,在config.js文件中,将此行添加到
require.config({
paths: {
// Make vendor easier to access.
"vendor": "../vendor",
// Almond is used to lighten the output filesize.
"almond": "../vendor/bower/almond/almond",
// add the requirejs text plugin here
"text" : "../vendor/bower/requirejs-text/text",
// Opt for Lo-Dash Underscore compatibility build over Underscore.
"underscore": "../vendor/bower/lodash/dist/lodash.underscore",
// Map remaining vendor dependencies.
"jquery": "../vendor/bower/jquery/jquery",
"backbone": "../vendor/bower/backbone/backbone"
}
});然后,要使用它,只需请求它,在本例中,您可以使用template变量访问它
define([
// These are path alias that we configured in our bootstrap
'app', // general app variables
'jquery', // lib/jquery/jquery
'underscore', // lib/underscore/underscore
'backbone', // lib/backbone/backbone
'text!templates/books.html' // use the plugin to import a template
], function(app,$, _, Backbone, template){ // don't forget to define it !发布于 2013-06-06 17:14:18
这就是我如何使用bower安装要求is text
在项目的bower.json文件中:
{
"name":"{{YOUR PROJECT NAME}}",
"version":"{{YOUR PROJECT VERSION}}",
"dependencies":{
"requirejs-text":"2.0.6"
}
}https://stackoverflow.com/questions/16514254
复制相似问题