【发布时间】:2020-04-29 14:47:28
【问题描述】:
我的索引书列出了所有已注册的书籍,当我单击编辑特定书籍时,它确实在路线中获得了特定书籍的 id,但我的 props 元素没有得到它,这是什么错误! 我该如何解决?
下面是我的索引页上的代码:
<router-link :to="{ name: 'update_book', params: {id: book.id} }">
<font-awesome-icon icon="pen-square" />
</router-link>
在我的更新页面中:
<script>
props: ['id'],
data() {
return {
books: [],
};
},
methods: {
getBook(id) {
axios.get(`http://localhost:3000/api/v1/books/${id}`) // Gets an specif book
.then((response) => { this.books = response.data; });
},
created() {
this.getBook(this.id);
},
watch: {
$route() {
this.getBook(this.id);
},
【问题讨论】:
-
你能显示你定义路线的代码吗?我的理解是,只有当它是“路由组件”时才会自动设置参数。请参阅示例here。
-
{ 路径:'/update_book/:id',名称:'update_book',组件:UpdateBook,道具:真,},
-
为什么要在那儿? params 不会向 props 传递任何东西
-
什么意思?
标签: vue.js vue-router vue-props