2017-05-06 25 views
0

在我app.component.ts我必須定義templateUrl就象這樣:相對URL具有角4和帆沒有工作

import {Component} from '@angular/core'; 
@Component({ 
    selector: 'root-component', 
    templateUrl: './app/app.component.html' 
}) 
export class AppComponent { } 

如果我定義它只是作爲./app.component.html它給這個錯誤控制檯: enter image description here

所以基本上這意味着app.component.html是accebile從http://localhost:1337/app/app.component.html,因爲我sailsroot/asset文件夾結構是這樣的:

enter image description here

這將是非常不方便寫templateUrls這種方式,因爲例如在asset/app/sidebar文件夾中的sidebar組件templateUrl會這樣寫:./app/sidebar/sidebar.component.html

我想在templateUrl相對URL以得到妥善解決我不知道該怎麼做。

如果有幫助,我遵循this教程。 (我所有的tsconfig.json,package.json等都與這裏給出的相同)。

+1

你應該使用'應用程序/ app.component.html' – Aravind

+0

@Aravind任何理由,爲什麼我應該用這種方式(即使我不希望) ? – rahulserver

+0

哪一個你使用'systemjs'或'webpack' – Aravind

回答

1

你可以嘗試的moduleId添加到裝飾這樣的:

@Component({ 
    moduleId: module.id, 
    selector: 'root-component', 
    templateUrl: './app/app.component.html' 
}) 

然而,根據angular.io更改日誌,你應該使用相對路徑和你沒有使用的moduleId,所以我驚訝你的代碼不起作用。

所有提到的moduleId被移除。 「組件相對路徑」 刪除(2017年3月13日)

食譜 我們添加了新SystemJS插件(systemjs-角loader.js)我們 推薦SystemJS配置。這個插件可以動態地將 templateUrl和styleUrls中的「組件相對」路徑轉換爲「絕對 路徑」。

我們強烈建議您僅編寫組件相對路徑。 是這些文檔中討論的唯一URL形式。你不再需要 寫@Component({moduleId:module.id}),你也不應該。

來源:https://angular.io/docs/ts/latest/guide/change-log.html