ngtemplate-loader
是一个用于 Webpack 的加载器(loader),专门用于处理 AngularJS (angular.js
) 的模板加载。它可以将 HTML 模板转换为 AngularJS 的 $templateCache
预编译缓存,避免运行时的额外 HTTP 请求,从而提高性能。
主要功能
预加载模板到
$templateCache
- 避免运行时的 AJAX 请求,提高应用加载速度。
支持
require()
语法- 允许在 AngularJS 组件或指令中通过
require()
直接引入模板文件。
- 允许在 AngularJS 组件或指令中通过
与 Webpack 兼容
- 可以与 Webpack 的
file-loader
或html-loader
结合使用。
- 可以与 Webpack 的
安装
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 中,从而提高页面加载性能。
示例 1:在指令中使用 ngtemplate-loader
angular.module('app').directive('myDirective', function() {
return {
restrict: 'E',
template: require('./my-directive.html'),
link: function(scope, element, attrs) {
console.log('指令初始化');
}
};
});
示例 2:动态注入模板
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('自定义组件加载');
}
});
示例 3:结合 Webpack 的 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 组件初始化');
}
});
示例 4:在 ui-router
路由配置中使用
angular.module('app').config(['$stateProvider', function($stateProvider) {
$stateProvider.state('home', {
url: '/home',
template: require('./home.html'),
controller: 'HomeController'
});
}]);
示例 5:在 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+。- 如果 HTML 模板路径不对,可能会导致
$templateCache
预加载失败,需要检查relativeTo
选项是否正确。 - 建议结合
html-loader
,否则 HTML 可能无法正确解析。
与 angular-templatecache-loader
的区别
ngtemplate-loader
适用于 Webpack,可以直接在 JavaScript 代码中require()
HTML。angular-templatecache-loader
可以批量将 HTML 添加到$templateCache
,适用于 手动注册多个模板 的情况。
如果你的项目使用 Webpack,ngtemplate-loader
是一个不错的选择!