首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >随Bower安装的RequireJS Text插件

随Bower安装的RequireJS Text插件
EN

Stack Overflow用户
提问于 2013-05-13 10:56:04
回答 3查看 11K关注 0票数 11

我应该如何使用通过bower安装的requirejs-text?我应该把它放在baseUrl中,但不知道我是否可以从components/requirejs-text/中使用它?最佳实践是什么?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-06-10 23:27:53

在配置中定义插件的路径:

代码语言:javascript
复制
requirejs.config({
    paths: {
        "text" : "components/requirejs-text/text"
    }
},

并按照https://github.com/requirejs/text上的文档在您的模块中使用它:

代码语言:javascript
复制
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中使用没有路径定义的插件,但这可能不是最佳实践:

代码语言:javascript
复制
require(["your_path_to_the_plugin_from_baseurl/without_js_at_the_end!some/textfile"],
    function(yourTextfile) {
    }
);
票数 25
EN

Stack Overflow用户

发布于 2013-10-08 00:28:31

PROJECT_APP/bower.js中,在dependencies部分下添加以下行:

代码语言:javascript
复制
"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文件中,将此行添加到

代码语言:javascript
复制
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变量访问它

代码语言:javascript
复制
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 !
票数 3
EN

Stack Overflow用户

发布于 2013-06-06 17:14:18

这就是我如何使用bower安装要求is text

在项目的bower.json文件中:

代码语言:javascript
复制
{
    "name":"{{YOUR PROJECT NAME}}",
    "version":"{{YOUR PROJECT VERSION}}",
    "dependencies":{
        "requirejs-text":"2.0.6"
     }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16514254

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档