使用相同的 Aurelia 路由基于动态变量动态选择视图模型的最佳实践?
Best practice to dynamically choose view model based on dynamic variable using the same Aurelia route?
我知道可以使用 navigationStrategy
动态指定 Aurelia 路由模块,但这对我来说不起作用,因为切换值驻留在仅运行一次的 RouterConfiguration 中。我需要一种路由到不同视图的方法,在这些视图中,切换值可以在一个会话期间多次更改。此外,并非所有路线都有多个视图。
根据动态值路由到同一路由上的不同视图的最佳做法是什么?
我想出了一些不同的策略,但我不确定哪一种是最可接受的方法。
Using viewPorts
其中路由将有一个静态 moduleId
到视图,该视图使用全局字符串将名称注入 <router-view name="view1_index"></router-view>
的实例,例如'view1' 可能被传递为 'view2',等等
使用 2 个或多个 <compose>
实例,其中路由将再次具有静态 moduleId
到将使用全局变量切换 if.bind
在 <compose>
个实例中
在路由模块中使用canActivate
,如果满足条件则重定向到辅助视口
在路由器配置中使用管道步骤来评估它是否应该指向不同的模块(如果可能的话)
以下哪种策略(如果有的话)最被接受?如果所有这些都是将每条路线路由到不同视图的奇怪方式,应该怎么办?
What is the best practice for routing to different views on the same
route based on a dynamic value?
Compose 是您最好的选择。通常将参数传递给路由,在 activate(params)
回调中捕获参数,使用参数在视图模型上设置变量,然后使用该变量设置 view-model
属性 <compose>
.
Using a pipeline step in the router config to evaluate whether it
should direct to a different module (if this is possible)
这是很有可能的。一个常见的用例是身份验证,您可以在其中使用 AuthorizeStep
检查用户是否获得授权,如果没有,则将其重定向。参见 http://foreverframe.net/exploring-aurelia-pipelines/。这也可以在 PreactivateStep
中激活。
Using viewPorts where the route will have a static moduleId to a view
that injects the name into an instance of using a global string, e.g. 'view1'
may be passed down as 'view2', etc.
除了将路由与屏幕特定部分的视图相关联之外,我建议不要将视口用于任何其他用途。
编辑:
您可能感兴趣的第三个选项是从 canActivate()
函数返回 Redirect
。
import { Redirect } from 'aurelia-router';
canActivate(params) {
let thing = this.thing = await this.service.getById(params.id);
if (!thing) {
return new Redirect('somewhere');
}
return true;
}
我知道可以使用 navigationStrategy
动态指定 Aurelia 路由模块,但这对我来说不起作用,因为切换值驻留在仅运行一次的 RouterConfiguration 中。我需要一种路由到不同视图的方法,在这些视图中,切换值可以在一个会话期间多次更改。此外,并非所有路线都有多个视图。
根据动态值路由到同一路由上的不同视图的最佳做法是什么?
我想出了一些不同的策略,但我不确定哪一种是最可接受的方法。
Using
viewPorts
其中路由将有一个静态moduleId
到视图,该视图使用全局字符串将名称注入<router-view name="view1_index"></router-view>
的实例,例如'view1' 可能被传递为 'view2',等等使用 2 个或多个
<compose>
实例,其中路由将再次具有静态moduleId
到将使用全局变量切换if.bind
在<compose>
个实例中在路由模块中使用
canActivate
,如果满足条件则重定向到辅助视口在路由器配置中使用管道步骤来评估它是否应该指向不同的模块(如果可能的话)
以下哪种策略(如果有的话)最被接受?如果所有这些都是将每条路线路由到不同视图的奇怪方式,应该怎么办?
What is the best practice for routing to different views on the same route based on a dynamic value?
Compose 是您最好的选择。通常将参数传递给路由,在 activate(params)
回调中捕获参数,使用参数在视图模型上设置变量,然后使用该变量设置 view-model
属性 <compose>
.
Using a pipeline step in the router config to evaluate whether it should direct to a different module (if this is possible)
这是很有可能的。一个常见的用例是身份验证,您可以在其中使用 AuthorizeStep
检查用户是否获得授权,如果没有,则将其重定向。参见 http://foreverframe.net/exploring-aurelia-pipelines/。这也可以在 PreactivateStep
中激活。
Using viewPorts where the route will have a static moduleId to a view that injects the name into an instance of using a global string, e.g. 'view1' may be passed down as 'view2', etc.
除了将路由与屏幕特定部分的视图相关联之外,我建议不要将视口用于任何其他用途。
编辑:
您可能感兴趣的第三个选项是从 canActivate()
函数返回 Redirect
。
import { Redirect } from 'aurelia-router';
canActivate(params) {
let thing = this.thing = await this.service.getById(params.id);
if (!thing) {
return new Redirect('somewhere');
}
return true;
}