【发布时间】:2020-10-31 21:11:57
【问题描述】:
我有一条路线:
router.js:
{
path: "/books/new/:title",
name: "new_book",
component: () =>
import('./components/user/NewBookView.vue')
}
我尝试通过路由器推送:
CreateBook.vue:
this.$router.push({
name: "new_book",
params: {
title: this.bookJson.data.title,
book: this.bookJson.data
}
});
但是我得到了一个
Error in nextTick: "TypeError:
Right-hand side of 'instanceof' is not callable"
错误。
我需要将bookJson 作为参数传递给我的NewBookView.vue 组件:
NewBookView.vue:
...
data: function() {
return {
book: JSON
}
},
created() {
this.book =
this.$route.params.book;
}
...
我试过了
this.$router.push({
path: "/books/new/" +
this.booksJson.data.title,
params: {
book: this.bookJson.data
}
});
但book 参数似乎是undefined。
所以似乎除非路由被命名,否则我无法传递参数,但如果它被命名,我也无法推送它。
编辑:我尝试将其更改为此,但仍然收到Error in nextTick: "TypeError: Right-hand side of 'instanceof' is not callable" 错误:
router.js:
{
path: "/books/new/:title",
name: "new_book",
component: () => import('./components/user/NewBookView.vue'),
props: route => ({
// set component property book to the route.params.book value
book: route.params.book,
...route.params
})
}
CreateBook.vue:
this.$router.push({
name: "new_book",
params: {
title: this.bookJson.data.title,
book: this.bookJson.data
}
});
})
NewBookView.vue:
export default {
props: {
book: JSON
},
created() {
this.props.book = this.$route.params.book;
}
}
【问题讨论】:
标签: javascript vue.js vue-component vue-router