Using DirectiveResolver to change @Component metadata

I am wondering how to use an object DirectiveResolverto modify a given one Component.

Dependencies (ng2 moves quickly these days, quickly become obsolete)

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

I tried this:

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 do not receive the logs, so I suspect that the decision has not been implemented.

Any idea?

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

+2
source share
1 answer

You need to pass additional providers by downloading the application to override the default compiler providers.

So, I think this should work:

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

Plunger example

+3
source

Source: https://habr.com/ru/post/1682386/


All Articles