我正在使用this。$ router.push以编程方式导航,但是,我被发送到错误的URL,我不确定为什么

时间:2019-09-29 13:03:26

标签: javascript vue.js vuejs2

我使用的是嵌套路由,因此可能会导致此问题,但是,我不确定如何解决此问题。我有以下路线和子路线:

{
    path: '/summoner/:summonerName',
    component: Summoner,
    children: [
        { path: '', component: Matchlist },
        { path: '/match/:matchId', component: SpecificMatch, name: 'specificMatch' }
    ]
},

当我在路径/summoner/:summonerName上时,我想查看默认的Summoner父组件和Matchlist组件;当我在路径/summoner/:summonerName/match/:matchId上时,我想查看默认的Summoner父级和specificMatch子级组件。但是,当我尝试使用时,效果很好:

this.$router.push({ name: 'specificMatch', params: { summonerName: this.summoner, matchId: matchId, summonerInfo: this.summonerInfo, match: match}})

我被发送到/match/:matchId路径而不是/summoner/:summonerName/match/:matchId,它破坏了组件,因为组件需要从路径中获取用户名。我以为this.$router.push会把我带到正确的路径,a。任何提示如何解决此问题?

1 个答案:

答案 0 :(得分:1)

这是绝对路径和相对路径的问题。

您的名称为'specificMatch'的路线具有指定的绝对路径'/match/:matchId',因此可以在这里导航。如果您希望将路径追加到父路径的路径,则必须使路径相对,这意味着省去了初始斜杠(/)-e.i. path: 'match/:matchId'