【问题标题】:How to fix template paths in templateCache? (gulp-angular-templatecache)如何修复模板缓存中的模板路径? (gulp-angular-templatecache)
【发布时间】:2015-10-06 03:39:13
【问题描述】:

我正在使用 gulp-angular-templatecache 生成一个 templateCache.js 文件,它将我所有的 HTML 模板文件合并为 1. (my full gulpfile)

将新模块注入我的应用程序后,我的指令将自动获取模板,我不需要将部分 .html 文件添加到我的构建文件夹中。

问题是前导文件夹路径被切断,请参见下面的示例:

我的指令中的路径:

templateUrl : "panels/tags/tagsPanel.html"...
templateUrl : "header/platform_header/platformHeader.html"...

我生成的templateCache文件中的路径:

$templateCache.put("tags/tagsPanel.html"...
$templateCache.put("platform_header/platformHeader.html"...

^ panelsheader 迷路了。


我正在尝试编写一个函数来修复我的 Gulpfile 中的问题。

我的Gulpfile的配置部分:

var config = {
    srcPartials:[
        'app/beta/*.html', 
        'app/header/**/*.html',
        'app/help/*.html',
        'app/login/*.html',
        'app/notificaitons/*.html',
        'app/panels/**/*.html',
        'app/popovers/**/*.html',
        'app/popovers/*.html',
        'app/user/*.html',
        'app/dashboard.html'
    ],
    srcPaths:[
        'beta/', 
        'header/',
        'help/',
        'login/',
        'notificaitons/',
        'panels/',
        'popovers/',
        'popovers/',
        'user/',
        'dashboard.html'
    ],
    destPartials: 'app/templates/'
};

我的html-templatesgulp.task

gulp.task('html-templates', function() {
    return gulp.src(config.srcPartials)
    .pipe(templateCache('templateCache.js', {
        root: updateRoot(config.srcPaths)
    },
    { module:'templateCache', standalone:true })
    ).pipe(gulp.dest(config.destPartials));
});

function updateRoot(paths) {
    for (var i = 0; i < paths.length; i++) {
        // console.log(paths);
        console.log(paths[i]);
        return paths[i];
    }
}

^ 以上是有效的,因为它使用gulp-angular-templatecache 中的root option 在模板路径前面附加一个新字符串。

问题是我上面的代码返回一次并更新所有路径到路径数组中的第一项,即beta/

您将如何编写它以正确替换每个文件的路径?

【问题讨论】:

    标签: javascript angularjs gulp angular-templatecache


    【解决方案1】:

    想通了!我不应该使用确切的文件夹名称,而是在我的配置中使用 glob **

    var config = {
        srcTemplates:[
            'app/**/*.html',            
            'app/dashboard.html',
            '!app/index.html'
        ],
        destPartials: 'app/templates/'
    };
    

    更新后的 gulp.task:

    gulp.task('html-templates', function() {
        return gulp.src(config.srcTemplates)
        .pipe(templateCache('templateCache.js', { module:'templateCache', standalone:true })
        ).pipe(gulp.dest(config.destPartials));
    });
    

    现在输出是正确的:

    $templateCache.put("beta/beta.html"...
    $templateCache.put("header/control_header/controlHeader.html"...
    $templateCache.put("panels/tags/tagsPanel.html"...
    

    【讨论】:

    • 我遇到了同样的问题并提出了同样的解决方案,但它不起作用。我的代码:pastebin.com/8Ub88fWA 谢谢你的帮助
    • 您是否需要注入或加载您创建的 templateCache 模块?
    • 是的,将其注入您的主应用模块
    • 我认为 app/**/*.html 和 app/**/**/*.html 一样,第一个双星已经在所有子目录中查找了。
    【解决方案2】:

    我遇到了类似的问题,但就我而言,不可能对所有 gulp.src 条目使用相同的目录。

    有一个适用于所有这些文件夹的解决方案

    return gulp.src(['public/assets/app1/**/*.tpl.html', 'public/assets/common_app/**/*.tpl.html'])
        .pipe(templateCache({
            root: "/",
            base: __dirname + "/public",
            module: "App",
            filename: "templates.js"
        }))
        .pipe(gulp.dest('public/assets/templates'))
    

    假设 gulpfile 是公共目录的上一级目录。它将从 src 文件的完整路径中删除基本字符串。 Base 也可以是一个将传递文件对象的函数,这在更复杂的情况下可能会有所帮助。它在 gulp-angular-templatecache 中 index.js 的第 60 行左右

    【讨论】:

    • 谢谢! __dirname + '/dir' 做了'./dir' 做不到的事情
    【解决方案3】:

    您还可以使用this gulp plugin,它可以读取您的路由、指令并将 templateUrl 替换为 templateUrl 中引用的模板。

    这将消除有关在您的应用程序中处理 templateUrl 的所有麻烦。这使用相对 url 来指示 js 文件,而不是绝对 url。

    src
    +-hello-world
      |-hello-world-directive.js
      +-hello-world-template.html
    

    hello-world-directive.js:

    angular.module('test').directive('helloWorld', function () {
        return {
            restrict: 'E',
            // relative path to template
            templateUrl: 'hello-world-template.html'
        };
    });
    

    hello-world-template.html:

    <strong>
        Hello world!
    </strong>
    

    gulpfile.js:

    var gulp = require('gulp');
    var embedTemplates = require('gulp-angular-embed-templates');
    
    gulp.task('js:build', function () {
        gulp.src('src/scripts/**/*.js')
            .pipe(embedTemplates())
            .pipe(gulp.dest('./dist'));
    });
    

    gulp-angular-embed-templates 将生成以下文件:

    angular.module('test').directive('helloWorld', function () {
        return {
            restrict: 'E',
            template:'<strong>Hello world!</strong>'
        };
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-19
      • 1970-01-01
      • 2017-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多