【发布时间】:2018-03-11 19:12:39
【问题描述】:
我正在使用 Vue/Vuex/Vue-router 创建一个单页应用程序。
基本上我试图从显示的列表中选择一条记录后检索它,我的商店基本上包括:
export const store = new Vuex.Store({
state: {
reports: null,
loading: false,
reportProcessing: false
},
getters: {
getReports (state) {
return state.reports
},
getReport (state) {
return (id) => {
return state.reports.find((item) => {
return item.id === id
})
}
}
}
// ...
当我尝试使用它时
data () {
return {
// Attempt to load the report by passing the current id
report: JSON.parse(JSON.stringify(this.$store.getters.getReport(this.id))),
// ...
它显示"SyntaxError: Unexpected token u in JSON at position 0" 的错误基本上返回一个空/空对象,这确实令人困惑,因为它有效(从对象列表中选择第一个元素):
JSON.parse(JSON.stringify(this.$store.getters.getReports[0])),
所以我知道对象列表包含报告(并且 getter 似乎运行正常)。但是,当尝试像 this.$store.getters.getReport(1) 那样手动传递 id 时,它不起作用。
我到底做错了什么?
编辑: 我当前的路由器文件设置为(用于单个报告路由)
{
path: '/report/:id',
props: true,
component: MainLayout,
children: [
{ path: '', name: 'edit_report', component: EditReport }
]
}
基本上,我使用 vue-router 的子路由将组件加载到具有主菜单的布局中,但是当我为该路由删除此功能时:
{
path: '/report/:id',
name: 'edit_report',
props: true,
component: EditReport
}
它起作用了(显然没有被加载到主布局中),不用说这不是一个修复(因为我仍然需要它像所有其他页面一样加载到主布局中),但也许它有一些关系我做错了什么?
【问题讨论】:
标签: vue.js vuejs2 single-page-application vue-router vuex