[英]Why passing data through Vue-router returning "[object Object]"?
What I'm trying to do is to pass some data from one component to another via router (I'm using Vue-Router-4 with Vue 3.0) .我想做的是通过路由器将一些数据从一个组件传递到另一个组件(我正在使用 Vue-Router-4 和 Vue 3.0) 。 But the data that I'm getting back is just "[object Object]" instead of the actual value.
但是我返回的数据只是“[object Object]”而不是实际值。 Here is what I did.
这是我所做的。 Inside the component that sends the data I have this function
在发送数据的组件内部,我有这个 function
goToQuestions() { this.$router.push({ name: "QuestionsPage", params: { data: this.questions }, }); },
inside my index.js I have this在我的 index.js 里面我有这个
{ path: "/questions-page", name: "QuestionsPage", props: true, component: () => import ('@/views/QuestionsPage.vue') },
And then inside the receiver component I have this snippet然后在接收器组件中我有这个片段
props: ["data"], mounted() { console.log("data coming from the router", this.data); },
I am assuming you this.questions
is either array or object.我假设您
this.questions
是数组或 object。
with router it is advised not to send objects and arrays. Instead send an id
.对于路由器,建议不要发送对象和 arrays。而是发送一个
id
。 And on the receiver page use that id
to fetch the data.并在接收者页面上使用该
id
来获取数据。
Even if you want to pass object you can try this:即使你想通过 object 你也可以试试这个:
when adding routes, use props's Function mode so that it has a default property user and it will add route.params as props.添加路由时,使用 props 的 Function 模式,这样它就有一个默认属性 user 并且它会添加 route.params 作为 props。
{
path: '/questions',
name: 'questions',
component: QuestionsComponent,
props: (route) => ({
user: userData,
...route.params
})
}
use this to push into your route用它来推进你的路线
self.$router.push({
name: 'questions',
params: {
data: this.questions
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.