How to set default activation activation in aurelia

Aurelia usually ignores any changes to the request.

You can set the activationStrategy value to invoke-lifecycle in the virtual machine to restart all life cycles in the virtual machine when the request changes.

To prevent clogging my code (placing it in each virtual machine), I want to set the default value of activationStrategy to invoke-lifecycle .

The interface explains that he but how to install it? https://github.com/aurelia/router/blob/master/src/interfaces.js

+7
aurelia aurelia-router
source share
2 answers

In ViewModel

(I also read your question incorrectly, but I leave it for completeness)

Place the determineActivationStrategy() method in the ViewModel and from there you can return the name or type of activation strategy that you want to use. Example:

 determineActivationStrategy() { return "invoke-lifecycle"; } 

The "invoke-lifecycle" or "replace" strings will work. You can also use the typed version by importing enum activationStrategy and keeping activationStrategy.replace / activationStrategy.invokeLifecycle . They work the same way.

At RouteConfig

Or, as Marton (who gave this answer before I did) stated, you can put it directly into the route configuration as an activationStrategy property.

This approach is better suited if the strategy does not depend on any particular state of your ViewModel model and you do not want to mask your presentation model with this material.

invoke-lifecycle vs. replace

In your question you say what you want

repeat all life cycles in VM

Please note that invoke-lifecycle reuses the existing ViewModel and will only refer to the router activation life cycle, which looks like this:

  • canDeactivate()
  • deactivate()
  • canActivate(params, routeConfig, navigationInstruction)
  • activate(params, routeConfig, navigationInstruction)

While replace throw away the existing ViewModel and call the entire ViewModel life cycle again on top of the router activation life cycle:

  • canDeactivate()
  • deactivate()
  • detached()
  • unbind()
  • (new instance): constructor()
  • canActivate(params, routeConfig, navigationInstruction)
  • activate(params, routeConfig, navigationInstruction)
  • created(owningView, thisView)
  • bind(bindingContext, overrideContext)
  • attached()

So, if you really want to run all the stages of the ViewModel life cycle, you need to use replace .

+21
source share

activationStrategy is a RouterConfig property that represents the route configuration object used by config.map() . I think you need to set it for each route definition.

Example:

 configureRouter(config, router) { ... config.map([ { route: ['', 'home'], name: 'home', moduleId: 'home/index', activationStrategy: 'invoke-lifecycle' } ]); ... } 

(Change the reason: I made a terrible mistake, first parsing your question incorrectly, sorry :))

+6
source share

All Articles