AngularJS 模板是html 文件,所以你需要添加一些加载器来处理它们。
1234563尺寸会变大。
这将允许您在控制器中“要求”您的 html 模板。
使用 copy-webpack-plugin 复制这些原始文件,这样它的工作方式与使用 Grunt 的方式相同(为复制的文件提供 templateUrl 路径)。
为了具体说明延迟加载的文件,您可以附加.lazy.html 后缀。
然后,在.lazy.html 文件上启用file-loader 并将其分配给templateUrl,并定期与template: require('template.html') 一起使用。
作为最佳实践,我会“要求”关键模板,以便它们位于 js 包中,并延迟加载(通过 templateUrl)非关键模板。
这是webpack.config.js 文件的示例:
module.exports = {
module: {
rules: [
{
test: /\.lazy\.html$/,
use: [
{
loader: 'file-loader',
options: {},
},
],
},
{
test: /\.html$/,
exclude: /\.lazy\.html$/
use: [
{
loader: 'html-loader',
options: {
minimize: true,
},
},
],
},
],
},
};
// critical.component.js
angular.
module('myApp').
component('greetUser', {
template: require('critical.html'),
controller: function GreetUserController() {
this.user = 'world';
}
});
// none-critical.component.js
angular.
module('myApp').
component('greetUser', {
templateUrl: require('non-critical.lazy.html'),
controller: function GreetUserController() {
this.user = 'world';
}
});