简体   繁体   English

如何在 vue.js 中的路由之间传递道具?

[英]How to pass props between routes in vue.js?

I want to send a variable as a prop from a route to another in vue.我想将变量作为道具从路由发送到 vue 中的另一个。

I have a vue component, Champions, using a v-for and creating a new component called ChampionCard for each character in a JSON file, and binding the current champion each time.我有一个 vue 组件 Champions,使用 v-for 并为 JSON 文件中的每个角色创建一个名为 ChampionCard 的新组件,并每次绑定当前的冠军。

In ChampionCard, I just show the name and image of the champion.在 ChampionCard 中,我只显示冠军的名字和形象。 I want that, when clicking on a ChampionCard, you get redirected to another component called Champion with URL champions/:champion.我想要这样,当点击 ChampionCard 时,你会被重定向到另一个名为 Champion 的组件,其 URL 为 Champions/:champion。

But I can't find a way to send the champion data between those 2 routes.但我找不到在这两条路线之间发送冠军数据的方法。

The Champions component :冠军组件:

<div class="champions">
    <ChampionCard v-bind:champion="champion" v-for="champion in champions" :key="champion.key"> 

    </ChampionCard>
 </div>

The ChampionCard component : ChampionCard 组件:

<article @click="redirectTo(champion)" class="champion">
      <img class="champion-icon" :src="champion.image" :alt="champion.name">
      <h2 class="champion-name"> {{ champion.name }} </h2>
</article>
redirectTo(champion) {
      this.$router.push({ name: 'champion', params: { champion: champion.key }})
    }

The above code is working as I get redirected to champions/championName, but I don't have access to my champion variable.当我被重定向到 Champions/championName 时,上面的代码正在运行,但我无法访问我的 Champion 变量。 What would be the best way to send it as a prop in Champion component ?将它作为 Champion 组件中的道具发送的最佳方式是什么?

Thanks :)谢谢 :)

My alternative is: While redirecting to champions/:champion route, just send the id of the champion (such as champions/123 ).我的替代方案是:在重定向到champions/:champion路线时,只需发送championid (例如champions/123 )。

When you are redirecting to this route, you can store champion object in a global place (say store ).当您重定向到此路线时,您可以将champion对象存储在全局位置(例如store )。 So that you can lookup with id (123) when you're mounting the route.这样您就可以在安装路线时使用 id (123) 进行查找。
One step further, if you cannot find the object in the global store, you can initiate a remote call.更进一步,如果在全局存储中找不到对象,可以发起远程调用。 So that your route will not be broken if 123 is not in the store.这样如果店里没有123 ,你的路线就不会中断。 (User can go directly to that route if they save some bookmarks of that page etc.) (如果他们保存了该页面的一些书签等,用户可以直接转到该路线。)

To keep your object in the global place, you have different alternatives:要将您的对象保持在全局位置,您有不同的选择:

  1. Simple State Management (Simple/Basic implementation)简单状态管理(简单/基本实现)
  2. Using Vuex使用Vuex

I think you can achieve what you want doing something like this:我认为你可以实现你想要做这样的事情:
Declare a new route:声明一条新路线:

{
  path: '/champion/:championKey',
  name: 'Some name',
  component: ChampionComponent,
}

Then, when redirecting to this new route:然后,当重定向到这条新路线时:

redirectTo(champion) {
  this.$router.push("/champion/" + champion.key)
}

Now you can access to the url param like this:现在您可以像这样访问 url 参数:

this.$route.params.championKey // as declared in router.js

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

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