简体   繁体   English

Vue路由器缺少参数

[英]Vue router missing params

I'm trying to use router link to pass a parameter to a page. 我正在尝试使用路由器链接将参数传递给页面。 The issue is that I'm not able to access the param playerId in the players page. 问题是我无法访问播放器页面中的参数playerId。 I've followed the example in vue router site . 我在vue路由器站点中遵循了该示例。 Below are my code. 下面是我的代码。

Route.js Route.js

{
      path: '/player/:slug',
      name: 'playerprofile',
      component: () => import(/* webpackChunkName: "playerprofile" */ './views/PlayerProfile.vue'),
      props: true
    }

Router Link Tag 路由器链接标签

<router-link :to="{path: '/player/'+ player.personName.replace(/ /g, '-').toLowerCase(), params: {slug: player.personName,playerId: player.personId}}">
            <TextH3 class="font-bold flex-1 text-black hover:text-primary">{{ player.personName }}</TextH3>
          </router-link>

Before Route hook in the page 页面中的Route钩子之前

beforeRouteEnter(to, from, next) {
        console.log(to)
        next()
    }

Console Log 控制台日志

fullPath: "/player/test-data"
hash: ""
matched: [{…}]
meta: {}
name: "playerprofile"
params:
  slug: "Test Data"
__proto__: Object
path: "/player/test-data"
query: {}

When you use path yours params will be equals to your slug in your case will be "slug: test-data" , there you should use query . 当您使用path您的params将等于您的slug在您的情况下将是"slug: test-data" ,在那里您应该使用query

If you want pass params you should use name instead of path as your link says. 如果您想要传递params ,则应使用name代替链接所指的path

update : This hack should work for you: 更新 :此hack应该为您工作:

<router-link
  :to="{
    name: 'playerprofile',
    params: {
      slug: player.personName.replace(/ /g, '-').toLowerCase(),
      playerName: player.personName,
      playerId: player.personId
    }
  }"
>
  <TextH3 class="font-bold flex-1 text-black hover:text-primary">
    {{ player.personName }}
  </TextH3>
</router-link>

Then u can access to params playerName & playerId . 然后,您可以访问参数playerNameplayerId

May this will help for you : 愿这对您有帮助:

created() {
    console.log(this.$route);
    this.slug = this.$router.params.slug;
  },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM