简体   繁体   English

如何仅将一个RequireJS配置文件用于不同的路径js文件?

[英]How to use only one RequireJS config file for different path js file?

I am integrating RequireJS into my project. 我正在将RequireJS集成到我的项目中。 My project structure like below: 我的项目结构如下:

$ tree
.
|-- common.js
|-- lib
|   |-- jquery.min.js
|   `-- require.js
`-- view
    |-- a
    |   |-- a.html
    |   |-- a.js
    |   `-- b
    |       |-- b.html
    |       `-- b.js
    |-- c.html   
    `-- c.js

common.js is my RequireJS config file, content like below: common.js是我的RequireJS配置文件,内容如下:

requirejs.config({
    baseUrl: '../lib',
    paths: {
        'jquery': 'jquery.min'
    }
});

There are js files in different path in /view . / view中的其他路径中有js文件。

c.js running well as below: c.js运行良好,如下所示:

requirejs(['../common'], function (common) {
    requirejs(['jquery'], function($){
    .......
    });
});

But how can I inject 'jquery' in a.js or b.js and still use common.js ? 但是,如何在a.jsb.js中注入“ jquery”并仍然使用common.js

Don't forget to change the path to common.js. 不要忘记将路径更改为common.js。 It should be: 它应该是:

requirejs(['../../common'], function (common) { ... });

from a.js and 从a.js和

requirejs(['../../../common'], function (common) { ... });

from b.js, since paths are relative to the current file. 来自b.js,因为路径是相对于当前文件的。

Alternatively, use an absolute path. 或者,使用绝对路径。 That way the path is the same no matter which file you're requirejs ing from. 这样的路径是相同的,无论哪个文件你requirejs从荷兰国际集团。 For example, if common.js is at http://www.example.com/scripts/common.js , use 例如,如果common.js位于http://www.example.com/scripts/common.js ,则使用

requirejs(['/common'], function (common) { ... });

. (The leading / makes it an absolute path.) (前导/使其成为绝对路径。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用相同的配置文件为带有requirejs的模块提供服务,以及如何在服务器端使用r.js进行串联和最小化? - How to use same config file for serving modules with requirejs and using r.js on the server side to concatenate and minify? 一个requirejs.config文件用于多个项目 - One requirejs.config file for several projects 如何在一个配置中为模板文件设置路径,例如requireJs中的文件,这样我就不必将模板路径URL传递给我的模块了? - How to set paths for template files in one config like file in requireJs so that I do not have to pass template path url to my module? 配置文件的RequireJS问题 - RequireJS issue with Config File 如何在node.js上使用require-js而不需要require(“ requirejs”)和在每个文件中重新定义requirejs? - How to use require-js on node.js without having to require(“requirejs”) and redefine requirejs in each file? Requirejs在define()里面加载不同的js文件 - Requirejs load different js file inside define() 使用requirejs优化器统一一个JS文件 - Uglify one JS file using requirejs optimizer 在 magento2 的 requirejs-config.js 中加载 js 文件 - load js file in requirejs-config.js in magento2 如何调试requireJS模块定义的路径/文件 - How to debug requireJS module defined path/file Magento 2-RequireJS配置文件未导入JS文件,未引发任何错误 - Magento 2 - RequireJS config file not importing JS files, no error thrown
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM