【发布时间】:2019-02-15 20:06:31
【问题描述】:
我在从 Vuex 商店中调度方法时遇到了问题? 这是出现的错误。
我只是在挂载的钩子中这样做:this.$store.dispatch('setSeason', seasonValue);
我在官方文档中看到了如何初始化store,但是不起作用...
这是我的 Vuex 商店初始化代码:
import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
Vue.use(Vuex)
var d = new Date();
var n = d.getMonth();
var current_season = (n == 1 || n == 2 || n == 12) ? 1 : (n == 3 || n == 4 || n == 5) ? 2 : (n == 6 || n == 7 || n == 8) ? 3 : 4;
const store = () => {
return new Vuex.Store({
state: {
locales: ['en', 'ru'],
locale: 'ru',
seasons: [{title: 'Зима', label: 'winter'}, {title: 'Весна', label: 'spring'}, {title: 'Лето', label: 'summer'}, {title: 'Осень', label: 'autumn'}],
season: current_season,
categories: {},
},
mutations: {
SET_LANG(state, locale) {
if (state.locales.indexOf(locale) !== -1) {
state.locale = locale
}
},
SET_SEASON(state, season){
if(season >0 && season <=4){
state.season = season
}
},
SET_CATEGORY(state, menu){
state.categories = Object.assign({}, menu)
}
},
actions: {
async nuxtServerInit({commit}){
let {data} = await axios.get("http://admin.duras.aws.kz/api/v1/categories", {
headers: {
'Content-Type': 'text/plain'
},
params: {
type: 'tree',
current_id: null
}
})
commit('SET_CATEGORY', data)
},
setSeason({commit}, value){
commit('SET_SEASON', value);
}
}
})
}
export default store;
【问题讨论】:
-
请提供更多信息和代码,在 Vue 实例上注册您的商店的代码和 setSeason 操作。
-
问题已被编辑)
-
你真的需要手动初始化存储吗?
-
但是我应该如何初始化呢?
标签: vue.js nuxt.js server-side-rendering