【发布时间】:2020-01-03 20:07:12
【问题描述】:
我需要在 store 中设置 currentId。我在组件中有它,在控制台中一切正常:
async created() {
const activityId = await this.$store.state.route.params.activityId
activityId: 'activityId'
console.log("here activityId: ", activityId)
console.log('print all params: ', this.$store.state.route.params)
console.log("STORE: ", this.$store.state)
},
我已将商店组织成模块,我正在处理的是 activity.js,它运行良好(我已将所有活动保存在商店中)。我现在需要设置当前的 id,然后根据它设置单个活动(这样我就可以访问它的数据)。 删除非固有代码,我所拥有的是:
import {
activityId
} from '@/views/ActivityDetail'
const state = {
currentActivityId: activityId
}
const mutations = {
SET_CURRENT_ACTIVITY_ID: (state, currentActivityId) => {
state.currentActivityId = currentActivityId
}
}
const actions = {
setCurrentActivityId({
commit
}) {
return new Promise(resolve => {
commit('SET_CURRENT_ACTIVITY_ID', '')
resolve()
})
}
}
export default {
namespaced: true,
state,
mutations,
actions
}
在模块“getters”中,我拥有的其他模块(工作正常):
activityId: state => state.activity.activityId,
还是activityId: undefined
我在路由器中有sync (store, router) 工作,还有mode: 'history',因为在此之前我尝试过:
import {router} from '@/router'
const state = {
currentActivityId: router.currentRoute.params.activityId,
}
我没有对mode:history做任何改变,所以我不知道问题是否可以在这里找到。但是评论它并使用 currentRoute 并没有解决问题。
我的应用中安装的版本是: “Vue路由器”:“3.0.6”, "vuex": "^3.1.0", "vuex-router-sync": "^5.0.0"
还是activityId: undefined
有人可以帮忙吗?
谢谢
【问题讨论】:
-
不清楚您的代码究竟是如何工作的。您要访问哪些属性以及在哪里访问它们?请尝试更好地组织您的问题。
-
据我所知,要使用 Vuex 存储数据,您应该使用
this.$store.commit('relative path to exact mutation method', your data) -
const activityId = ...在这里使用 const 而不是let似乎很奇怪.. -
您的商店看起来有
state.currentActivityId。那么,activityId: state => state.currentActivityId? -
@Wes Doyle 是的,你是对的,但它并没有解决问题..
标签: vue.js vuejs2 vuex vue-router vuex-modules