2017-02-23 20 views
2

我在想如何使用DirectiveResolver對象來更改給定的Component使用DirectiveResolver更改@Component元數據

相關性(NG2動作如此之快,這些天事情就變得過時快速)

"@angular/common": "~2.4.3", 
"@angular/compiler": "~2.4.3", 
"@angular/core": "~2.4.3", 

我已經試過這樣:

import { Component } from '@wwwalkerrun/nativescript-ngx-magic'; 
import { OnInit, Directive, Type } from '@angular/core'; 
import { DirectiveResolver } from '@angular/compiler'; 


class myViewResolver extends DirectiveResolver { 
    resolve(type: Type<any>, throwIfNotFound?: boolean): Directive {   
     var view = super.resolve(type, throwIfNotFound); 
     console.log(type); 
     console.log(view); 
     return view; 
    } 
} 

@Component({ 
    selector: 'app-root', 
    templateUrl: 'views/app/app.component.html', 
    styleUrls: ['views/app/app.component.css'], 
    providers: [myViewResolver] 
}) 
export class AppComponent { 
    title = 'app works!'; 
} 

但讓我懷疑解析器是我不明白的日誌沒執行。

有什麼想法?

PS:官方angular.io API文檔中沒有條目...

回答

3

您需要通過引導應用程序覆蓋默認的編譯器供應商將額外的供應商。

所以我覺得這個應該工作:

platformBrowserDynamic().bootstrapModule(AppModule, { 
    providers: [ 
    { provide: DirectiveResolver, useClass: myViewResolver } 
    ] 
}); 

Plunker Example

+0

完美,謝謝 – Sebas

+0

@yurzui:你知道,如果一個人能夠覆蓋'templateUrl's也以這種方式?在'DirectiveResolver'結尾的'view'似乎已經有一個獲取模板的'template'屬性... –

+1

@VolkerRose你的意思是這個https://plnkr.co/edit/IOWpMZDzR3Kjiie0zfKA?p=preview ? – yurzui