ngtemplate-loader
是一个用于 Webpack 的加载器(loader),专门用于处理 AngularJS (angular.js
) 的模板加载。它可以将 HTML 模板转换为 AngularJS 的 $templateCache
预编译缓存,避免运行时的额外 HTTP 请求,从而提高性能。
预加载模板到 $templateCache
支持 require()
语法
require()
直接引入模板文件。与 Webpack 兼容
file-loader
或 html-loader
结合使用。npm install ngtemplate-loader --save-dev
在 Webpack 配置文件 (webpack.config.js
) 中添加 ngtemplate-loader
:
module.exports = {
module: {
rules: [
{
test: /\.html$/,
use: [
'ngtemplate-loader?relativeTo=/src', // 预加载到 $templateCache
'html-loader' // 解析 HTML
]
}
]
}
};
在 AngularJS 组件或指令中使用 require()
方式引入模板:
angular.module('app').component('myComponent', {
template: require('./my-component.html'),
controller: function() {
console.log('组件加载成功');
}
});
上面的 require('./my-component.html')
会自动将 my-component.html
的内容存入 $templateCache
,这样 AngularJS 运行时就不需要额外的 HTTP 请求。
可以通过几种方法来判断 $templateCache 是否已经生效:
在浏览器控制台中检查
打开浏览器的开发者工具,在控制台执行下面的命令:
angular.element(document.body).injector().get('$templateCache')
这会返回一个对象,你可以查看这个对象的键名列表,确认你的模板(例如 layout_show.html)的路径或标识是否存在。
检查网络请求
打开浏览器的 Network 面板,然后刷新页面。如果你的模板已经通过 ngtemplate-loader 提前缓存,那么对应的模板文件就不会发起额外的 HTTP 请求。如果没有看到相关请求,就说明模板已经从 $templateCache 中加载了。
在代码中调试
可以在 AngularJS 的控制器或配置中注入 $templateCache,然后打印它的内容。例如:
ngModule.run(['$templateCache', function($templateCache) {
console.log('Cached templates:', Object.keys($templateCache));
}]);
这样在应用启动时,你就能在控制台看到当前缓存了哪些模板。
通过上述方法,就可以判断你的模板是否已经成功缓存到 $templateCache 中,从而提高页面加载性能。
ngtemplate-loader
angular.module('app').directive('myDirective', function() {
return {
restrict: 'E',
template: require('./my-directive.html'),
link: function(scope, element, attrs) {
console.log('指令初始化');
}
};
});
angular.module('app').run(['$templateCache', function($templateCache) {
$templateCache.put('custom-template.html', require('./custom-template.html'));
}]);
angular.module('app').component('customComponent', {
templateUrl: 'custom-template.html',
controller: function() {
console.log('自定义组件加载');
}
});
html-loader
module.exports = {
module: {
rules: [
{
test: /\.html$/,
use: [
'ngtemplate-loader?relativeTo=/src/views',
'html-loader'
]
}
]
}
};
然后在 AngularJS 组件中:
angular.module('app').component('dashboard', {
template: require('./views/dashboard.html'),
controller: function() {
console.log('Dashboard 组件初始化');
}
});
ui-router
路由配置中使用angular.module('app').config(['$stateProvider', function($stateProvider) {
$stateProvider.state('home', {
url: '/home',
template: require('./home.html'),
controller: 'HomeController'
});
}]);
ng-include
里使用<div ng-include="'views/partial.html'"></div>
Webpack 配置:
module.exports = {
module: {
rules: [
{
test: /\.html$/,
use: [
'ngtemplate-loader?relativeTo=/src/views',
'html-loader'
]
}
]
}
};
这样 views/partial.html
就会被 ngtemplate-loader
预加载到 $templateCache
里。
这些示例涵盖了组件、指令、路由、ng-include
等常见场景,你可以根据自己的需求选择合适的方式使用。
ngtemplate-loader
适用于 AngularJS (1.x),而不是 Angular 2+。$templateCache
预加载失败,需要检查 relativeTo
选项是否正确。html-loader
,否则 HTML 可能无法正确解析。angular-templatecache-loader
的区别ngtemplate-loader
适用于 Webpack,可以直接在 JavaScript 代码中 require()
HTML。angular-templatecache-loader
可以批量将 HTML 添加到 $templateCache
,适用于 手动注册多个模板 的情况。如果你的项目使用 Webpack,ngtemplate-loader
是一个不错的选择!