如何在 angular.dart 中为路由参数分配默认值? [英] How to assign default values for routing parameters in angular.dart?
问题描述
考虑以下路由配置:
route
..addRoute(
name: 'custom',
defaultRoute: true,
path: '/:param1/:param2',
enter: view('view/template.html')
);
我正在将路由器注入控制器并设置 router.onRouteStart.listen
,在其中我通过 router.activePath.last.parameters
访问参数,现在,如果 param2
不存在于用户输入的路由中,那么我想为 param2
分配一个默认值(也替换路由).我该怎么做?
I am injecting the router into a controller and setting up router.onRouteStart.listen
, within which I am accessing parameters via router.activePath.last.parameters
, now, if param2
is not present in the user entered route, then i want to assign a default value to param2
(also replace the route). How do I go about it?
推荐答案
经过更多探索,在 angular.dart.tutorial 上找到了这个示例:https://github.com/angular/angular.dart.tutorial/blob/master/Chapter_06/lib/routing/recipe_book_router.dart#L19-L24
After a bit more of exploration found this sample on angular.dart.tutorial: https://github.com/angular/angular.dart.tutorial/blob/master/Chapter_06/lib/routing/recipe_book_router.dart#L19-L24
有以下方法(根据我的示例代码进行更改):
Which has the following way of doing it (changes according to my sample code):
route
..addRoute(
name: 'default',
defaultRoute: true,
path: '/:param1',
enter: (RouteEnterEvent e) =>
router.go('custom', {
'param1': router.activePath.last.parameters['param1'],
'param2': 'defaultValue'},
//[Optional, if mounted in a nested route]
//startingFrom: router.root.findRoute('custom_head'),
replace: true))
..addRoute(
name: 'custom',
defaultRoute: true,
path: '/:param1/:param2',
enter: view('view/template.html')
);
如果用户没有为 param2 提供值,那么第一个路由将被激活,它将为 param2 分配一个默认值并通过替换先前的来强制更新 url.
If the user doesn't provide a value for param2 then the first route gets activated, which will assign a default value for param2 and force update of url by replacing earlier one.
这篇关于如何在 angular.dart 中为路由参数分配默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!