使用DirectiveResolver

使用DirectiveResolver

我想知道如何使用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:没有正式的angur.IO API文档中的条目…

最佳答案

您需要通过引导应用程序来传递额外的提供程序,以覆盖默认的编译器提供程序。
所以我认为这应该管用:

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

Plunker Example

08-08 04:03