【发布时间】:2013-05-07 00:38:23
【问题描述】:
我应该如何使用通过凉亭安装的requirejs-text?我应该把它放在baseUrl 但想知道我是否可以从components/requirejs-text/ 使用它?最佳做法是什么?
【问题讨论】:
标签: configuration requirejs bower
我应该如何使用通过凉亭安装的requirejs-text?我应该把它放在baseUrl 但想知道我是否可以从components/requirejs-text/ 使用它?最佳做法是什么?
【问题讨论】:
标签: configuration requirejs bower
这就是我使用 bower 安装 requirejs-text 的方式
在您项目的 bower.json 文件中:
{
"name":"{{YOUR PROJECT NAME}}",
"version":"{{YOUR PROJECT VERSION}}",
"dependencies":{
"requirejs-text":"2.0.6"
}
}
【讨论】:
baseUrl,但由于它在凉亭components,我该如何使用它?
requirejs-text。
在配置中定义插件的路径:
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) {
}
);
【讨论】:
在PROJECT_APP/bower.js的依赖项下添加这一行:
"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 !
【讨论】:
"vendor": "../vendor" 如何帮助供应商“更容易访问”?在任何情况下,您都不需要供应商目录。