我做了很多研究,但我似乎没有找到确切的答案......
我的问题很简单;我有一条路线,我们称之为“父/子”,如果没有提供参数,我想在其中设置默认参数(param/child?param1=xxx¶m2=xxx 等等)。
我的 URL 中经常需要参数,因为它允许我更新页面上依赖于这些参数的一些图表。我还希望它们出现在 URL 中,而不是出现在 localeStorage/Session 或服务中,因为我的用户很容易通过复制/粘贴来共享页面。
我试过很多东西,比如:
{
path: 'parent',
component: Parent,
children: [
{
path: 'child',
redirectTo: 'child:param1:param2'
},
{
path: 'child:param1:param2',
component: Child
},
但它不起作用。我考虑过我的组件之间的共享服务,以便在未提供这些参数时设置这些参数,但在那之后我想我应该做一些类似 route.navigate([myLocation], myParams) 的事情来更改带有参数的 URL。 所以这意味着每次我都需要检索我的当前位置?
如果有人可以帮助我,我有点困惑。谢谢!
编辑:我的 app.routes:
{
path: 'parent',
component: Parent,
children: [
{
path: 'child',
component: Child
},
]
},
{
path: '**',
redirectTo: '/parent/child'
}
对不起,如果这是一个愚蠢的问题,那一定是语法错误...
最佳答案
只需添加如下 URL:
{
path: 'child',
redirectTo: 'child/a/b'
},
其中 "a"
是 param1
的值,"b"
是 param2
关于Angular 2 RC6 - 在 URL 中设置默认参数并重定向到带有参数的路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39352822/