【问题标题】:RequireJS Text Plugin installed with Bower与 Bower 一起安装的 RequireJS 文本插件
【发布时间】:2013-05-07 00:38:23
【问题描述】:

我应该如何使用通过凉亭安装的requirejs-text?我应该把它放在baseUrl 但想知道我是否可以从components/requirejs-text/ 使用它?最佳做法是什么?

【问题讨论】:

    标签: configuration requirejs bower


    【解决方案1】:

    这就是我使用 bower 安装 requirejs-text 的方式

    在您项目的 bower.json 文件中:

    {
        "name":"{{YOUR PROJECT NAME}}",
        "version":"{{YOUR PROJECT VERSION}}",
        "dependencies":{
            "requirejs-text":"2.0.6"
         }
    }
    

    【讨论】:

    • 我的意思是问我如何配置 requireJS 以使用文本插件。我知道它的意思是放在应用程序的baseUrl,但由于它在凉亭components,我该如何使用它?
    • bower 仅帮助您将所有文件(是的整个 github)拉到本地环境。您可能仍需要配置另一个工具来进一步提取它。这种工具的一个例子是grunt-bower-task。之后,像往常一样在requirejs.config中配置文本插件。
    • 除了文本插件,还可以考虑 requirejs-tpl 插件github.com/jfparadis/requirejs-tpl 使用起来更方便
    • 没有回答正确的如何使用的问题,而是如何安装它
    • 嗯,2.0.10 版本的名字并不是真正的requirejs-text。
    【解决方案2】:

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

    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) {
        }
    );
    

    【讨论】:

      【解决方案3】:

      在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" 如何帮助供应商“更容易访问”?在任何情况下,您都不需要供应商目录。
      猜你喜欢
      • 1970-01-01
      • 2014-10-29
      • 2014-07-21
      • 2014-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多