7

The Angular Routing docs mention component instance creation, component instance activation, and route activation.

The docs do not explain the differences of these concepts, and when each creation/activation occurs.


Questions

  1. What is the difference between instance creation and instance activation?
  2. What is the difference between instance activation and route activation?
  3. Does instance activation always occur at the same time as instance creation?

In summary: It is not clear what is really meant by component instance activation and route activation, and how that relates to component instance creation (particularly timing wise).


Known Information

Instance Creation

  • Component instances are created by Angular when navigating between components of different types
  • When navigating between instances of the same component, the instances are re-used by default

Instance Activation

  • When browser's location URL changes to match a path segment (e.g /crisis-center), Router activates an instance of corresponding component (e.g CrisisListComponent) and displays its view
  • When app requests navigation to a path (e.g /crisis-center), Router activates instance of corresponding component (e.g CrisisListComponent), displays its view, and updates browser's address location and history with URL for that path

Route Activation

  • Mentioned a few places throughout the docs. See below

Angular Doc References

Here are some mentions of the above three concepts, in the Angular docs:

Instance Creation

By default, the router re-uses a component instance when it re-navigates to the same component type without visiting a different component first.

...

This application won't re-use the HeroDetailComponent. The user always returns to the hero list to select another hero to view. There's no way to navigate from one hero detail to another hero detail without visiting the list component in between. Therefore, the router creates a new HeroDetailComponent instance every time.

Link

Instance Activation

When the browser's location URL changes to match the path segment /crisis-center, then the router activates an instance of the CrisisListComponent and displays its view.

Link

When the application requests navigation to the path /crisis-center, the router activates an instance of CrisisListComponent, displays its view, and updates the browser's address location and history with the URL for that path.

Link

Route Activation

The data property in the third route is a place to store arbitrary data associated with this specific route. The data property is accessible within each activated route.

Link

You can also protect child routes with the CanActivateChild guard. The CanActivateChild guard is similar to the CanActivate guard. The key difference is that it runs before any child route is activated.

Link

In the Hero Detail and Crisis Detail, the app waited until the route was activated to fetch the respective hero or crisis.

Link

The ActivatedRouteSnapshot contains the future route that will be activated and the RouterStateSnapshot contains the future RouterState of the application, should you pass through the guard check.

Link

Community
  • 1
  • 1
Magnus
  • 6,791
  • 8
  • 53
  • 84
  • Can you please provide a link where the docs mention that? – Günter Zöchbauer Jan 08 '18 at 18:11
  • @GünterZöchbauer Yep, sure. Now added – Magnus Jan 08 '18 at 18:31
  • *mention instance creation, instance activation, and route activation* - they don't. You operate with terms that are neither valid nor easy to understand without given context. Notice that the text you're citing doesn't mention them. The thing you're asking about is likely *route component instance creation vs route activation*, meaning that properly asked question needs to know half the answer. – Estus Flask Jan 08 '18 at 18:57
  • @estus I now added bold to the block quotes, for increased clarity. The fact that there is an "activated route" implies that "route activation" happened at some point. Same goes for "instance creation" and "instance activation" (docs state that the "router activates an instance", but not what that really means.). In summary, it is not clear what is really meant by instance/route activation, and how that relates to component instance creation. – Magnus Jan 08 '18 at 19:09
  • These are component instances that are created. These are routes that are activated. At least that's what semantics suggests. A route and route component instance have one-to-one relationship, so they are used interchangeably in docs, even if this sounds wrong at times. I'm not up to full-fledged answer, but briefly, route component is instantiated on first route activation and then an instance can be reused on subsequent activations. The way it's reused is defined by RouteReuseStrategy provider. – Estus Flask Jan 08 '18 at 19:35
  • Docs did not explicitly state this, but I am guessing that a route gets activated when a user navigates to that specific route (e.g root-component/42/foo/some-other-req-parameter), and that a route is considered "active" as long as user resides at that particular route. The docs kind of stated the first part of that. Then, after each successful navigation, Router creates a tree of ActivatedRoute objects, representing each level in the active route. In my example, the tree would hold these objects: root-component, 42, foo, some-other-req-parameter. This is pure speculation though. – Magnus Jan 08 '18 at 20:16
  • Also, another partial guess: When navigating between instances of different components, instances are created and activated at the same time. When re-navigating between instances of the same component, instances are not re-created, but routes are activated/deactivated – Magnus Jan 08 '18 at 20:31

1 Answers1

14

What is the difference between instance creation and instance activation?

Instantiating means creating an instance of a route (ActivateRoute) or component. Activating of a route means attaching it to the router-outlet directive. Activating of a component means attaching it to the DOM. Routes and components are activated using activateWith function of a router-outlet directive.

Let's see some examples. Suppose you have the following routes:

{
    path: 'a',
    component: AComponent,
    children: [
        { path: 'b', component: BComponent },
        { path: ':name', component: DComponent }
    ]
}

Now you navigate to a/b.

The router will:

  • instantiate { path: 'a', component: AComponent, children: [] } route
  • instantiate { path: 'b', component: BComponent } route
  • activate these routes by attaching them to the respective router-outlet locations
  • instantiate AComponent and BComponent using this approach
  • activate AComponent and BComponent by adding them to DOM

Now you navigate to a/n1.

The router will:

  • reuse route for a - { path: 'a', component: AComponent, children: [] } route (no instantiation or activation)
  • instantiate { path: ':name', component: DComponent } route
  • activate { path: ':name', component: DComponent } route
  • reuse AComponent instance (no instantiation or activation)
  • instantiate DComponent instance
  • activate DComponent by attaching it to the router-outlet in AComponent view

Now you navigate to a/n2.

The router will:

  • reuse route for a - { path: 'a', component: AComponent, children: [] } route (no instantiation or activation)
  • reuse route for n2 - { path: ':name', component: DComponent } route (no instantiation or activation)
  • update params for the n2 activated route
  • reuse DComponent instance (no instantiation or activation)
Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488
  • Thank you, that is incredibly useful information. I have no idea why the docs do not describe these processes better. Would you say that my two "guesses" in the comments to my post, are more or less right? – Magnus Jan 08 '18 at 20:56
  • PS: The "this approach" link is not working. Any chance you could update it? Thanks again. – Magnus Jan 08 '18 at 23:14
  • @Magnus, I've updated my answer and added more explanation to the steps. See if it makes sense now. Also, it's not clear what you mean by _Does instance activation always occur at the same time as instance creation?_ What is `same time`? – Max Koretskyi Jan 09 '18 at 07:12
  • Ahh, I see Max. It seems that whenever there is an instantiation event, there is always an activation event, and wise versa. The re-use concept that the docs present regarding component instances, also applies to routes and to activation. So, both an instance and an activation is re-used when visiting instances of the same component. When navigating between components of different types, instances are re-created and re-activation also occurs. Did I get that right? Sorry for all the back and forth – Magnus Jan 09 '18 at 12:46
  • 1
    @Magnus, yeah. Usually instance creation and activation go hand in hand. – Max Koretskyi Jan 09 '18 at 16:18
  • @MaxKoretskyiakaWizard, I think the last point ```reuse DComponent instance (no instantiation or activation)``` needs review as the constructor of DComponent is called every time so there will be a new instantiation and activation for the component and hence the components can't be reused. Please explain if I'm missing something. – patrick.1729 Oct 10 '19 at 18:18