我浏览了这个有点相关的vue-router@4.05 对象参数。在该线程中提到了这个https://github.com/vuejs/vue-router-next/issues/494,但我仍然很困惑。
我还尝试使用路由器链接组件的:to属性传递一些数据。我做了两支笔来展示这个问题:
笔 1成功传递了在路线定义中定义的对象。
Pen 2试图传递一个动态对象,而不是实际对象,它得到一个带有 [Object object] 的字符串,如 github 问题中所述。
控制台输出:
[Vue 警告]:无效的道具:道具“存储库”的类型检查失败。预期的对象,得到具有>值“[对象对象]”的字符串。at <Repository repository="[object Object]" onVnodeUnmounted=fn ref=Ref > at at at at
所以,如果我明白这一点,最终你不能传递一个动态对象,因为它被解析了,但你可以传递一个静态对象?
我已经尝试过 props: true 和一切,我正在尝试功能模式解决方案作为更复杂的示例
片段:
<router-link :to="{ name: 'Home' }">Home</router-link>
<router-view />
<router-link
:to="{
name: 'Repository',
params: { repository: { one: '1', two: '2' } },
}">click me</router-link>
v1
const routes: Array<RouteRecordRaw> = [
{
path: "/",
name: "Home",
component: Home
},
{
path: "/repo/",
name: "Repository",
component: () => import("../components/Repository.vue"),
props: (route) => {
console.log("entered route");
console.log(route);
return { ...route.params, repository: { one: "1", two: "2" } };
}
}];
v2
const routes: Array<RouteRecordRaw> = [
{
path: "/",
name: "Home",
component: Home
},
{
path: "/repo/",
name: "Repository",
component: () => import("../components/Repository.vue"),
props: (route) => {
console.log("entered route");
console.log(route);
return { ...route.params };
}
}];