使用DirectiveResolver更改@Component元数据 [英] Using DirectiveResolver to alter @Component metadata

查看:112
本文介绍了使用DirectiveResolver更改@Component元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用DirectiveResolver对象更改给定的Component.

I am wondering how to use the DirectiveResolver object to alter a given Component.

依赖性(ng2如今移动得如此之快,事情很快就过时了)

Dependencies (ng2 moves so fast these days things become obsolete quickly)

"@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!';
}

但是我没有得到日志,所以我怀疑解析器未执行.

But I don't get the logs so I suspect the resolver is not executed.

有什么主意吗?

ps:没有正式的angular.io api文档条目...

ps: no entry in the official angular.io api documentation...

推荐答案

您需要通过引导应用程序传递额外的提供程序以覆盖默认的编译器提供程序.

You need to pass extra providers by bootstrapping application to override default compiler providers.

所以我认为这应该可行:

So i think this should work:

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

柱塞示例

Plunker Example

这篇关于使用DirectiveResolver更改@Component元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆