2016-09-18 62 views
2

我想使用UrlResolver在我的模板中使用組件相對URL。使用UrlResolver獲取組件相對URL

我已經實現UrlResolver在我的代碼如下描述:「不!提供商UrlResolver」 http://dfbaskin.com/posts/angular2-url-resolver-for-component-assets/

但每次瀏覽器控制檯只是顯示錯誤。

我對Angular完全陌生。任何想法如何解決這個問題?

btw:我在使用SystemJS。

我component.ts:

import {Component} from "@angular/core"; 
import {UrlResolver} from "@angular/compiler"; 

@Component({ 
    moduleId: module.id, 
    templateUrl: "my-template.html" 
}) 

export class PageNotFoundComponent { 
    constructor(private urlResolver: UrlResolver) { 
    } 

    public resolvePath(path) { 
     return this.urlResolver.resolve(module.id, path); 
    } 
} 

在我的模板(我-template.html)我有以下HTML:

<img [src]="resolvePath('some-resource.png')"/> 
+0

它會幫助我們之間的非精神病患者顯示產生此錯誤的代碼行... –

+0

我已經爲我的問題添加了一個示例。 – Programie

+0

哪個版本的angular2使用? –

回答

1

直接回答你的問題,你應該添加providers: [UrlResolver]到你的@Component。

像這樣:

@Component({ 
    moduleId: module.id, 
    templateUrl: "my-template.html", 
    providers: [UrlResolver] 
}) 

但是,這對我來說沒有給出解決的主要問題 - 如何讓相對路徑的資源。 因爲然後它開始抱怨「沒有提供令牌應用程序包根URL!」。

也許你至少可以進一步推動。

+0

您是否能找到「沒有令牌應用程序包根URL的提供商」的解決方案!問題,我打破了我的頭來解決這個問題:( –