0

I have added current component selector in app.component.ts as per my previous question Angular2, How to get the current active component selector to use it as class globally for css styling

but now i have modified my code to use lazy module functionality, I have only issue of accessing the component selector using the below code

 activeSelector: string;

 constructor(private resolver: ComponentFactoryResolver) {}

 onActivated(component) {
 this.activeSelector = 
      this.resolver.resolveComponentFactory(component.constructor).selector;    

 }

I am getting the below error:

No component factory found for MyComponent. Did you add it to @NgModule.entryComponents?.

I have tried to add in the entry components and in NgModule even but still don't know how to access it for lazy module.

Does any one know how to achieve it for lazy module. Thank in advance for the help.

mpunit30
  • 381
  • 9
  • 26
  • Can you setup demo? You should use ComponentFactoryResolver from lazy module – yurzui Jan 27 '18 at 04:33
  • OK. I will do that and give you the Link – mpunit30 Jan 27 '18 at 05:04
  • Here is the link : https://stackblitz.com/edit/angular-rtp7pg You can check the code in app.component.ts. I want to access the current component as selector in app.component.html as i have applied css based on the selector. When i tried to access the component selector i facing error No component factory found for Component – mpunit30 Jan 27 '18 at 05:41
  • Here's workaround https://stackblitz.com/edit/angular-rtp7pg-r53hby?file=src/app/app.component.ts for the being time – yurzui Jan 27 '18 at 05:58
  • I am getting this error : ERROR TypeError: Cannot read property '0' of undefined at AppComponent.onActivated (app.component.ts:77) at Object.eval [as handleEvent] (AppComponent.html:15) – mpunit30 Jan 27 '18 at 06:18
  • Which version angular are you using? – yurzui Jan 27 '18 at 06:18
  • I am using angular-cli v4.3.3 – mpunit30 Jan 27 '18 at 06:21
  • I am getting the constructor when i use this and console. Object.getPrototypeOf(component).constructor but when i do Object.getPrototypeOf(component).constructor['__annotations__'] i am getting undefined console.log – mpunit30 Jan 27 '18 at 06:39
  • Because it was changed. Here's the previous version https://stackoverflow.com/questions/45274539/when-and-how-s-decorator-applied-to-the-decorated-classes-from-the-angular-pack/45276490#45276490 of how to get metadata from component. But it's too ugly for me anyway – yurzui Jan 27 '18 at 06:42
  • Ok great one it worked. Thanks @yurzui for the help. it saved a lot of time. But i want your suggesstion. what's the best practice should i use this method? wil it effect produciton issues ?? or aot build ? should i migrate to cli v.5 to use the latest methods like the pne you defined in stacckblitz? – mpunit30 Jan 27 '18 at 06:58
  • Here's another option https://stackblitz.com/edit/angular-rtp7pg-39hsz9?file=src/app/app.component.ts You can get tagName directly – yurzui Jan 27 '18 at 07:05
  • Thanks yurzui. But this is again breaking. @tagName. But previous code works fine, NextElementSiblings is returning me as undefined – mpunit30 Jan 27 '18 at 07:22
  • Maybe the best approach that will absolute work with Jit and AOT is to create key (like `cssClass`) on your component https://stackblitz.com/edit/angular-rtp7pg-cwjxaa?file=src%2Fapp%2Fapp.component.ts – yurzui Jan 27 '18 at 07:26
  • This is better option. Easy and quite better. Thanks great one again @yurzui. Great knowledge !!! – mpunit30 Jan 27 '18 at 07:29

0 Answers0