六、requirejs配置

六、 requirejs配置

配置:

本身具有这种功能,需要开启, 所以要进行配置

requirejs提供了config方法用于配置模块的

6.1 paths

作用:

简化路径

值是一个对象

1
2
key: 新的路径
value: 原始路径
1
2
3
4
5
6
7
8
9
10
11
// 使用config配置
requirejs.config({
// 配置paths
paths: {
"module": "module/header"
}
})

define(["module/header"], function(header) {
console.log(header);
})

6.2 shim

作用:

将文件转为模块

值是一个对象

1
2
key: 模块路径
value 是一个配置对象
1
2
配置接口 exports
配置依赖集合 deps
1
2
3
4
5
6
7
8
9
10
11
12
13
// 使用config配置
requirejs.config({
// 配置shim
shim: {
"lib/jquery-1.12.2": {
// 配置接口
exports: "$",
deps: []
}
}
})

define(["module/header/header"]);

6.3 map

作用:

配置模块文件

属性是一个路径

value 是一个对象

1
2
key: 引入的模块文件
value: 修改的模块文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// 使用config配置
requirejs.config({
// 配置shim
shim: {
"lib/jquery-1.12.2": {
// 配置接口
exports: "$",
// 配置依赖集合
deps: []
},
"lib/jquery100": {
// 配置接口
exports: "$",
// 配置依赖集合
deps: []
}
},
// 配置map
map: {
"module/header": {
"lib/jquery": "lib/jquery-1.12.2"
},
"module/footer": {
"lib/jquery": "lib/jquery100"
}
}
})

define(["module/header/header", "module/footer/footer"]);

6.4 baseUrl

作用:

更改根目录的

优先级关系:

baseUrl > data-main > require

6.5 css插件

requirejs不能直接引入css文件

requirejs中引入css要当做模块来引入,需要配置

1
2
3
4
5
6
// 在模块文件中要加上css!前缀
map {
“*”: {
css: 文件目录
}
}
点击查看

本文标题:六、requirejs配置

文章作者:Mango

发布时间:2020年07月14日 - 13:08:55

最后更新:2020年07月14日 - 13:27:10

原始链接:https://mango185.github.io/post/8c44889.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

-------------------本文结束 感谢您的阅读-------------------