使用DirectiveResolver更改@Component元数据

时间:2017-02-23 10:27:12

标签: angular typescript

我想知道如何使用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文档中没有条目...

1 个答案:

答案 0 :(得分:3)

您需要通过bootstrapping应用程序传递额外的提供程序来覆盖默认的编译器提供程序。

所以我认为这应该有效:

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

<强> Plunker Example

相关问题