[英]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.js或b.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.