vue.js - 如何通过参数传递整个对象
问题描述
我是 vue 路由器的新手,不明白如何在页面链接中填充帖子
<router-link :to{name: "componentName"}></router-link>
我尝试使用如下对象但没有成功
<router-link :to="{name: 'componentName', params: {
book: {
slug: book.slug,
title: book.title,
ratings: book.rating,
author: book.author,
synopsis: book.synopsis
}
}}">
并收到
<template>
<div>
<h1>{{this.$route.params.book}}</h1>
</div>
</template>
<script>
export default {
data(){
return{
book: {
book: this.$route.params.book,
}
}
},
}
</script>
<style lang="scss" scoped>
</style>
我做错了什么或者有更好的方法吗?
解决方案
您可以在路线上使用道具模式,props: true
在声明路线时添加:
const routes = [
{
name: "book",
path: "/book",
component: Book,
props: true // <-- Add this
}
];
const router = new VueRouter({
routes,
mode: "history"
});
new Vue({
router,
render: (h) => h(App)
}).$mount("#app");
并在您的Book
页面上添加props
:
<script>
// BookPage.vue
export default {
name: "Book",
props: {
book: Object, // <--- Add this
},
};
</script>
然后简单地传递使用params
你的router-link
<router-link :to="{name: 'book', params: {
book: {
slug: book.slug,
title: book.title,
ratings: book.rating,
author: book.author,
synopsis: book.synopsis
}
}}">
演示:https ://codesandbox.io/s/pensive-sunset-vgoy5?file=/src/App.vue
阅读有关路由器道具的更多信息:https ://router.vuejs.org/guide/essentials/passing-props.html
推荐阅读
- shopify - 从部分外部访问部分块字段
- ssis - 由于错误 0xC0011008“从 XML 加载时出错,SSIS 包无法加载
- python - 从 keras 转换为 pytorch
- docker - 在新的 docker 容器中运行安装 nodejs 的命令有效,但在 Dockerfile 中失败?
- excel - =IFERROR(IF(MIN(Milestones[Start])=0,TODAY(),MIN(Milestones[Start])),TODAY()),在 Milestones[Start] 范围内抛出错误
- excel - 无法对 VBA SaveCopyAs 使用正确的路径
- python - 安装了第三方 python 模块 - 仍然不会导入(windows)
- sql - 在 SQL Server 中为每个客户生成行项目的顺序 ID
- apache-spark - 如何在 PySpark 中优化这个 groupby 语句以避免缓冲区错误(shuffle block size > 2GB)?
- python - 无法加载 spaCy 'en' 模块