2016-10-29 79 views
0

我試圖構建一個新的角度2,我遇到了一個問題,其中我的組件模板正在投擲404錯誤,我不能爲我的生活找出原因。Angular 2 + mvc core:無法加載資源:服務器響應狀態爲404(Not Found)

我可以配置一個組件並指定模板,但由於當前正在逃離我的原因,我一直在控制檯中看到模板被引用爲.js文件。

比如我看到下列錯誤:

Error: (SystemJS) XHR error (404 Not Found) loading http://localhost:5000/app/app.html.js [email protected]http://localhost:5000/node_modules/zone.js/dist/zone.js:698:34 [email protected]http://localhost:5000/node_modules/zone.js/dist/zone.js:265:40 [email protected]http://localhost:5000/node_modules/zone.js/dist/zone.js:154:57 [email protected]http://localhost:5000/node_modules/zone.js/dist/zone.js:335:40 Error loading http://localhost:5000/app/app.html.js as "app/app.html" from http://localhost:5000/app/app.component.js

現在我知道我沒有一個app.html.js,但我想不出爲什麼角試圖加載的文件不存在。進一步的只會增加混亂時,我知道我明確設置我的模板app.html這樣

import { Component } from "@angular/core"; 
import { Router } from "@angular/router"; 

const template = require("./app/app.html"); 

@Component({ 
    selector: "alerts", 
    template: template 
}) 

export class AppComponent { 
    constructor(public router: Router) {} 
} 

任何援助將不勝感激。

編輯 只是爲了去除外部模板排除我還簡化了它的成分,直接把模板標記到組件,我仍然會得到相同的結果,角度是尋找一個文件,那既不引用或現有:

import { Component } from "@angular/core"; 
import { Router } from "@angular/router"; 

@Component({ 
    selector: "alerts", 
    template: `<alerts></alerts>` 
}) 

export class AppComponent { 
    constructor(public router: Router) {} 
} 

回答

0

更簡單的方法來做到這一點是使用了moduleId:module.id,,因此不需要要求

@Component({ 
    moduleId:module.id, // this makes the .html relative to the folder you are in 
    selector: "alerts", 
    templateUrl: app.html 
}) 
相關問題