如何通过angular2中的id引用模板

How to refer to a template by id in angular2?

本文关键字:引用 id 中的 何通过 angular2      更新时间:2023-09-26

我正试图找到一种方法,通过使用唯一的名称而不是文件夹路径来引用文件。使用绝对路径和相对路径,如果我移动组件文件夹,我必须重构所有链接。这也很烦人,因为通过使用gump,所有的javascript文件都被移动到另一个目录。

在Angular 1中,我们只是指ng-template的ID。

在Angular 2中有没有类似的方法?

附加信息:

代码我目前正在使用

(function(app) {
    app.AppComponent =
        ng.core.Component({
            selector: 'my-app-test',
            templateUrl: '/assets/base/components/comp-test-app/test-template.html'
        })
            .Class({
                constructor: function() {}
            });
})(window.app || (window.app = {}));

通过设置组件的moduleId,可以通过相对url引用模板。

(function(app) {
    app.AppComponent =
        ng.core.Component({
            moduleId: module.id,
            selector: 'my-app-test',
            templateUrl: 'test-template.html'
        })
        .Class({
            constructor: function() {}
        });
})(window.app || (window.app = {}));

来源:https://angular.io/docs/ts/latest/cookbook/component-relative-paths.html