Gulp Angular 构建模板缓存问题

Kou*_*nha 5 javascript angularjs gulp

build.js正在使用ngHtml2js将指令中的部分转换为 js 文件。

gulp.task('partials', function () {
  return gulp.src('src/{components,app}/**/*.html')
    .pipe($.ngHtml2js({
      moduleName: 'testApp'
    }))
    .pipe(gulp.dest('.tmp'))
    .pipe($.size());
});
Run Code Online (Sandbox Code Playgroud)

其中,我的指令位于 components->directives 中的 components 文件夹中。

问题是,当我尝试部署 dist 文件夹时,来自路由 JS 文件的静态部分调用被触发,并且我得到 404。

angular.module(ModuleName)
        .directive('collapseWindow', ['$parse', function (parse) {

            return {
                templateUrl:'/components/directives/collapse.html',
                transclude:true,
                restrict: 'A'
Run Code Online (Sandbox Code Playgroud)

在我的理解中,ngHtml2js 将部分转换为 module.run 块作为

module.run(['$templateCache', function($templateCache) {
  $templateCache.put('components/directives/collapse.html',
    '<div class="collapsible">\n' ...
Run Code Online (Sandbox Code Playgroud)

但是,为什么我会收到 404 错误,例如

 GET http://localhost:3000/components/directives/collapse.html 404 (Not Found)
Run Code Online (Sandbox Code Playgroud)

Jay*_*Jay 1

您需要将模板模块添加为 Angular 应用程序模块中的依赖项。还要确保您的模板缓存 js 文件已在应用程序代码之前加载到浏览器中。