javascript - 如何将参数传递给未命名的路由:TypeError:'instanceof' 的右侧不可调用
问题描述
我有一条路线:
路由器.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
组件:
新书视图.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"
错误:
路由器.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
}
});
})
新书视图.vue:
export default {
props: {
book: JSON
},
created() {
this.props.book = this.$route.params.book;
}
}
解决方案
使用道具功能模式
像这样设置你的路由器:
{
path: "/books/new/:title",
name: "book",
component: () => import("../pages/Book.vue"),
props: route => ({
// set component property book to the route.params.book value
book: route.params.book,
...route.params
})
}
而不是 using data
,props
请在您的组件上使用:
export default {
name: "home",
props: {
title: String,
book: Object
}
};
并像这样推送到您的路线:
this.$router.push({
name: "book",
params: {
title: "Foo Bar",
book: {
id: 1,
title: "Foo Bar",
author: "John Doe"
}
}
});
演示:https ://codesandbox.io/s/vue-router-demo-3cfqs?file=/pages/Home.vue
推荐阅读
- excel - 如何将多个文本框值合并到 1 个文本框中,除了跳过空值和 vbNewLine
- java - 如何访问 HttpMessageNotReadableException 中的 _path 属性
- python-3.x - 如何在 jupyter notebook 中获取绘图图?
- sql - 如何在 R 函数中发送不同数量的变量以供 sql 查询使用
- matlab - Matlab:制作9行相同的值而不存储所有9个值
- sql - Laravel Builder 中的 1 where 子句中的 2 条件
- c++ - 无符号和有符号的隐式转换
- node.js - 如何在 VueJs 中将我的搜索显示到我的表中?
- php - 为什么 AJAX 会导致 PHP 会话崩溃?
- java - 在数据库中存储自定义日期