如何在Aurelia中设置默认激活策略 [英] How to set default activationStrategy in aurelia

查看:93
本文介绍了如何在Aurelia中设置默认激活策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Aurelia通常会忽略查询字符串中的任何更改.

Aurelia normally ignores any changes in the querystring.

可以在VM中将activationStrategy设置为invoke-lifecycle,这样当查询字符串更改时,它将重新运行VM中的所有生命周期.

It is possible to set the activationStrategy to invoke-lifecycle in the VM so it will re-run the all the life cycles in the VM when the querystring changes.

为防止乱码(将其放置在每个VM中),我想将默认的activationStrategy设置为invoke-lifecycle.

To prevent littering my code (placing it in every VM) i want to set the default activationStrategy to invoke-lifecycle.

在界面中解释了它可行的,但是如何设置呢? https://github.com/aurelia/router/blob/master/src/interfaces.js

In the interface it's explained that it is possible, but how to set it? https://github.com/aurelia/router/blob/master/src/interfaces.js

推荐答案

在ViewModel上

(我一开始也误读了您的问题,但为了完整起见,我将其保留)

On the ViewModel

(I misread your question at first too, but I'm leaving this in for completeness)

在ViewModel上放置方法determineActivationStrategy(),然后从那里可以返回要使用的激活策略的名称或类型.示例:

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

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

字符串"invoke-lifecycle""replace"将起作用.您还可以通过导入枚举activationStrategy并重试activationStrategy.replace/activationStrategy.invokeLifecycle来使用键入的版本.它们的工作原理相同.

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

或者,正如Marton(在我之前给出这个答案的人)所说的那样,您可以将其直接作为属性activationStrategy放在路由配置中.

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

如果该策略不依赖于ViewModel的任何特定状态并且您不希望用这些东西乱扔视图模型,则此方法更合适.

This approach is better suited if the strategy does not depend on any particular state of your ViewModel and you don't wish to litter your view model with this stuff.

在您的问题中,您说您想

In your question you say you want to

重新运行VM中的所有生命周期

re-run the all the life cycles in the VM

请注意,invoke-lifecycle将重用现有的ViewModel,并将仅调用路由器激活生命周期,如下所示:

Note that invoke-lifecycle reuses the existing ViewModel and will only invoke the router activation lifecycle, which is as follows:

  1. canDeactivate()
  2. deactivate()
  3. canActivate(params, routeConfig, navigationInstruction)
  4. activate(params, routeConfig, navigationInstruction)
  1. canDeactivate()
  2. deactivate()
  3. canActivate(params, routeConfig, navigationInstruction)
  4. activate(params, routeConfig, navigationInstruction)

replace replace将丢弃现有的ViewModel,然后再次在路由器激活生命周期的顶部调用整个ViewModel生命周期:

Whereas replace will throw away the existing ViewModel and invoke the whole ViewModel lifecycle again on top of the router activation lifecycle:

  1. canDeactivate()
  2. deactivate()
  3. detached()
  4. unbind()
  5. (新实例):constructor()
  6. canActivate(params, routeConfig, navigationInstruction)
  7. activate(params, routeConfig, navigationInstruction)
  8. created(owningView, thisView)
  9. bind(bindingContext, overrideContext)
  10. attached()
  1. canDeactivate()
  2. deactivate()
  3. detached()
  4. unbind()
  5. (new instance): constructor()
  6. canActivate(params, routeConfig, navigationInstruction)
  7. activate(params, routeConfig, navigationInstruction)
  8. created(owningView, thisView)
  9. bind(bindingContext, overrideContext)
  10. attached()

因此,如果您确实要运行ViewModel生命周期的所有步骤,则需要使用replace.

So if you really want to run all the ViewModel lifecycle steps, you'll need to use replace.

这篇关于如何在Aurelia中设置默认激活策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆