0
  1. Can you check the existence of an instance of a component from the component itself ?

  2. Can you destroy existing instance and create a new instance ?

I seem to have a problem with one of my components. When visited multiple times consecutively with different params component's ngAfterViewInit fires only first time (i assume when the component was instantaited). Subsequent visits doesnt cause to fire ngAfterViewInit, ie, looks as if it is reusing the same component instance or something... however, I can identify the params and do the needful with the changed params..

However, If I visit a new route and come back to the route/compoent in question it works normal. and I can see it is getting destroyed before the new route and corresponding component initiated.

My objective is to re-instatiate the component EVERY TIME I vist the route.

So

  1. Can you check the existence of an instance of a component from the component itself ?

  2. Can you destroy existing instance from code and create a new instance ?

Or is there any other way to tackle this issue.

My setup

Lazy loaded module -> mdm

const routes: Routes = [

  {
    path: '',
    component: HomeComponent,

    children : [
                  { path: ':id' , component : AComponent }
    ]
  }

Menu AnchorLinks

  1. mdm/1000
  2. mdm/2000 etc etc
Niladri
  • 5,832
  • 2
  • 23
  • 41
user2533922
  • 85
  • 1
  • 14

1 Answers1

0

There's definitely another way of doing that.

To answer you, you can probably check the existence of a component instance, and you can probably re-create it on demand.

But you should not.

Not only this is overkill, but you also should let Angular manage the injectors, otherwise (if you don't know what you're doing), you'll encounter some funny issues.

To explain it briefly to you : reloading a route doesn't trigger the lifecycle of a component. A component's lifecycle starts when he is instantiated, and ends when he is destroyed (which means "not in the DOM anymore").

When you reload a route, the component doesn't get destroyed, hence your issue.

To resolve that, subscribe to route change events in your lifecycle hook :

ngAfterViewInit() {
  this.router.events
    .pipe(filter(event => event instanceof NavigationEnd))
    .subscribe(event => /* do something */);
}

I have piped the observable to only listen to the last event of the stack (otherwise, your subscription will be ran a lot of times).

  • thanx for that.. will try to incorporate into the code.. since my component is somewhat complex in the sense it it dynamically creates TABS, form controls, populate SELECT options (thru DOM manipulation) with data calls etc.. all from metadata from backend..its a dynamic (based on param on the route) CRUD component.. i think it is best to DESTROY the existing instance(maybe in ctor) and let angular take care of the rest.. ie, simulating navigation to a different url (thereby destroying the existing instance of the component).. what do you think and how can achieve that ? – user2533922 Dec 19 '18 at 12:53
  • I think you're wrong and you should not do that. You have a design flaw in the sense that if you need to dynamically create something, it shouldn't be done in the constructor (or in Angular case, lifecycle hooks). I'm giving you the best practice to do what you want : what you are asking is [help on something you think will work, but you have no idea if it really will](http://xyproblem.info/). –  Dec 19 '18 at 14:32
  • @trichetriche..I think you misunderstood me... CTOR will only destory the existing instance and perhaps initialize some variables...obviously i wont do heavy lfiting in ctor.. The things are already in working condition...however, i am rethinking and as you say trying to bring in best practices.. – user2533922 Dec 19 '18 at 14:55
  • Oh no I get you very well, don't worry about that. I'll present it to you differently : you are trying to override the router (which is working great) to accomodate your need. You're trying to rewrite how Angular works (and has been working for half a decade now) because you can't find how to do what you want. See how it sounds bad ? Instead, you should, as you said, implement best practices and review your own code before reviewing Angular's behavior. (Sorry if this offended you, that wasn't my goal) –  Dec 19 '18 at 15:12
  • @ trichetriche..not at all..i am not offended at all..nor i am looking to rewrite angular way.. I am exploring angular to port one of my large application..so prototyping this complex component and learn more about Angular innards ..i can understand why Angular hold on to the component instance till the last possible moment ..however, it would be better if there is a way for the programmer to intervene or configure to tell the framework to re-instantiate a component on revisitng the same route. Framework does it anyway if you visit another route.. – user2533922 Dec 19 '18 at 16:38