微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

angularjs – systemjs-builder:Angular 2 Component Relative Paths导致404错误

这是 https://github.com/systemjs/builder/issues/611的交叉帖子

我正在尝试使用systemjs-builder 0.15.16 buildStatic方法捆绑我的Angular 2 rc 1应用程序.角度组件具有视图以及脚本外部的一个或多个样式表.它们在one of two ways中的@Component元数据中引用

使用绝对路径

@Component({
  templateUrl: 'app/some.component.html',styleUrls:  ['app/some.component.css']
})

使用现在推荐的相对路径:

@Component({
  moduleId: module.id,templateUrl: 'some.component.html',styleUrls:  ['some.component.css']
})

我的应用程序使用相对路径,事情一直很好.今天我决定使用systemjs-builder的buildStatic.每当存在相对路径时,生成文件都会抛出404错误,因为浏览器正在获取localhost / some.component.html而不是localhost / app / some.component.html.下面是我的gulpfile.js的相关部分

var appDev = 'dev/';
var appProd = 'app/';
var typescript = require('gulp-typescript');
var tsProject = typescript.createProject('tsconfig.json');
var Builder = require('systemjs-builder');
var sourcemaps = require('gulp-sourcemaps');

gulp.task('build-ts',function () {
    return gulp.src(appDev + '**/*.ts')
        .pipe(sourcemaps.init())
        .pipe(typescript(tsProject))
        .pipe(sourcemaps.write())
        .pipe(gulp.dest(appProd));
});
gulp.task('bundle',['build-ts'],function() {

    var builder = new Builder('','./systemjs.config.js');

    return builder
        .buildStatic(appProd + '/main.js',appProd + '/bundle.js',{ minify: false,sourceMaps: true})
        .then(function() {
            console.log('Build complete');
        })
        .catch(function(err) {
            console.log('Build error');
            console.log(err);
        });
});

使用相对路径,如果我只运行build-ts gulp任务并浏览“常规”方式,那么事情就可以了.如果我运行bundle gulp任务并尝试使用bundle.js文件浏览应用程序,那么在应用程序尝试加载外部模板和样式表的任何地方都会出现404错误.我试图通过将templateUrl:’some.component.html’更改为templateUrl:’./ some.component.html’无效来明确路径的相对性质.硬编码绝对路径似乎是一个坏主意.我错过了什么?

几天后,我从github上的systemjs成员获得了 a helpful response.

诀窍是什么:在systemjs-builder的buildStatic方法的配置对象中,将encodeNames设置为false.所以线……

.buildStatic(
    appProd + '/main.js',sourceMaps: true}
)

成为…

.buildStatic(
    appProd + '/main.js',sourceMaps: true,encodeNames:false}
)

附加信息

tsconfig.json

{
  "compilerOptions": {
    "target": "ES5","module": "commonjs","moduleResolution": "node","sourceMap": true,"emitDecoratorMetadata": true,"experimentalDecorators": true,"removeComments": false,"noImplicitAny": false,"outDir": "./app"
  },"filesGlob": [
    "./dev/**/*.ts","!./node_modules/**/*.ts"
  ],"exclude": [
    "node_modules","typings"
  ]
}

原文地址:https://www.jb51.cc/angularjs/142431.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐