Angular 2需要html.js文件作为模板

时间:2017-01-15 13:48:04

标签: angular systemjs angular2-template

我的angular2应用程序遇到了很多麻烦。

我有一个非常简单的mvc应用程序(我使用的是asp.net)。 enter image description here

然而,我得到了一个 当我尝试在我的innertest组件中使用模板时,我的应用程序无法加载以下消息。

来自' @ angular / core&#39 ;;

的内部组件代码`import {Component}
@Component({
    selector: 'test',
   template: require('./innerhome.component.html')
})
export class InnerTestComponent {
    name = '';
}
`

消息为Failed to load resource: the server responded with a status of 404 (Not Found) localhost/:20 Error: (SystemJS) XHR error (404 Not Found) loading http://localhost:2802/app/components/content/innerhome.component.html.js Error: XHR error (404 Not Found) loading http://localhost:2802/app/components/content/innerhome.component.html.js at XMLHttpRequest.wrapFn [as _onreadystatechange] (http://localhost:2802/node_modules/zone.js/dist/zone.js:698:29) at ZoneDelegate.invokeTask (http://localhost:2802/node_modules/zone.js/dist/zone.js:265:35) at Zone.runTask (http://localhost:2802/node_modules/zone.js/dist/zone.js:154:47) at XMLHttpRequest.ZoneTask.invoke (http://localhost:2802/node_modules/zone.js/dist/zone.js:335:33) Error loading http://localhost:2802/app/components/content/innerhome.component.html.js as "./innerhome.component.html" from http://localhost:2802/app/components/content/innertest.component.js at XMLHttpRequest.wrapFn [as _onreadystatechange] (http://localhost:2802/node_modules/zone.js/dist/zone.js:698:29) at ZoneDelegate.invokeTask (http://localhost:2802/node_modules/zone.js/dist/zone.js:265:35) at Zone.runTask (http://localhost:2802/node_modules/zone.js/dist/zone.js:154:47) at XMLHttpRequest.ZoneTask.invoke (http://localhost:2802/node_modules/zone.js/dist/zone.js:335:33) Error loading http://localhost:2802/app/components/content/innerhome.component.html.js as "./innerhome.component.html" from http://localhost:2802/app/components/content/innertest.component.js

我的systemjs.config.js文件是

(function (global) {
System.config({
    paths: {
        // псевдоним для пути к модулям
        'npm:': 'node_modules/'
    },
    // указываем загрузчику System, где искать модули
    map: {
        // наше приложение будет находиться в папке app
        app: 'app',
        // пакеты angular
        '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
        '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
        '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
        '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
        '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
        '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
        '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
        '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
        '@angular/upgrade': 'npm:@angular/upgrade/bundles/upgrade.umd.js',
        // остальные пакеты
        'rxjs': 'npm:rxjs',
        'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js'
    },
    // пакеты, которые указывают загрузчику System, как загружать файлы без имени и расширения
    packages: {
        app: {
            main: './main.js',
            defaultExtension: 'js'
        },
        rxjs: {
            defaultExtension: 'js'
        }
    }
});

})(本);

我不明白这个问题的原因。

请告知。

1 个答案:

答案 0 :(得分:1)

在装饰器中,您应该使用templateUrl而不是template。只要您引用的文件位于同一文件夹中,就应该只使用文件名,而不是路径:

templateUrl: 'innerhome.component.html'

每次指向组件装饰器中的另一个文件时,您还需要包括:

moduleId: module.id,

并确保在app.module声明中有InnerTestComponent。

整个组件将如下所示:

从@ angular / core';

导入{Component}
@Component({
  moduleId: module.id,
  selector: 'test',
  templateUrl: 'innerhome.component.html'
})
export class InnerTestComponent {
  name = '';
}