【问题标题】:Vue: redirect to straight to child, while parent has a paramVue:直接重定向到孩子,而父母有一个参数
【发布时间】:2018-11-26 22:05:15
【问题描述】:

我正在学习 Vue(在 TS 上),到目前为止它看起来很棒。目前我正在学习 Vue 路由,并且有一个问题我很难找到一个漂亮的答案。

假设我有一个名为 User 的父路由,它将 userId 作为参数。我也有这条路线的子页面,分别称为 Profile 和 Settings,并在 User 的 children 数组中设置:

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'});
}

但我的问题是 - 我可以以相同的方式从用户组件的 outside 重定向到用户的个人资料,而无需像

那样连接路径字符串
public redirectToProfile() {
    this.$router.push({ path: 'user/' + userId + '/profile'});
}

?

【问题讨论】:

  • 我不这么认为。

标签: redirect vue.js vuejs2 vue-router


【解决方案1】:

我认为您也可以通过传递参数来简单地做到这一点:

public redirectToProfile(id: string) {
   this.$router.push({ name: 'Profile', params: {userId:id}});
}

【讨论】:

    猜你喜欢
    • 2014-12-06
    • 1970-01-01
    • 2020-08-28
    • 1970-01-01
    • 2019-07-23
    • 1970-01-01
    • 1970-01-01
    • 2022-08-03
    • 2017-05-21
    相关资源
    最近更新 更多