我正在学习Vue(在TS上),到现在为止似乎还很棒。目前,我了解了Vue路由,并遇到了一个问题,我很难找到一个漂亮的答案。
比方说,我有一个名为User的父路由,该路由将userId作为参数。我也有此路由的子页面,分别称为“配置文件”和“设置”,并在“用户的子项”数组中进行设置:
routes: [
{
component: User,
name: 'User'
path: '/user/:userId',
children: [
{
component: Profile,
name: 'Profile',
path: 'profile',
},
{
component: Settings,
path: 'settings',
name: 'Settings'
},
],
}
]
我可以像
一样简单地将用户组件重定向到配置文件或设置,这很酷public redirectToProfile() {
this.$router.push({ name: 'Profile'});
}
但是我的问题是-我可以用相同的方式从User组件的外部重定向到用户的个人资料,而不用像
那样连接路径字符串吗?public redirectToProfile() {
this.$router.push({ path: 'user/' + userId + '/profile'});
}
?
答案 0 :(得分:0)
我认为您也可以通过传递参数来做到这一点:
public redirectToProfile(id: string) {
this.$router.push({ name: 'Profile', params: {userId:id}});
}