Vue.js Can';t mounted()hook Nuxt.js中的Vuex存储中的分派方法
我在从Vuex存储区中调度该方法时遇到问题? 这是一个出现的错误 我只是在mountedhook中执行此操作:Vue.js Can';t mounted()hook Nuxt.js中的Vuex存储中的分派方法,vue.js,nuxt.js,server-side-rendering,Vue.js,Nuxt.js,Server Side Rendering,我在从Vuex存储区中调度该方法时遇到问题? 这是一个出现的错误 我只是在mountedhook中执行此操作:this.$store.dispatch('setSeason',seasionvalue) 我在官方文档中看到了如何初始化存储,但它不起作用 这是我的Vuex存储初始化代码: import Vue from 'vue' import Vuex from 'vuex' import axios from 'axios' Vue.use(Vuex) var d = new Date()
this.$store.dispatch('setSeason',seasionvalue)代码>
我在官方文档中看到了如何初始化存储,但它不起作用
这是我的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”导入Vue
从“Vuex”导入Vuex
从“axios”导入axios
Vue.use(Vuex)
var d=新日期();
var n=d.getMonth();
当前季节变量=(n==1 | | n==2 | | n==12)?1:(n==3 | | n==4 | | n==5)?2:(n==6 | | n==7 | | n==8)?3 : 4;
常量存储=()=>{
返回新的Vuex.Store({
声明:{
地区:['en','ru'],
地区:'ru',
季节:[{标题:“冬季”},{标题:“春季”},{标题:“夏季”},{标题:“冬季”},标签:“秋季”},
季节:本季,
类别:{},
},
突变:{
设置语言(状态、区域设置){
if(state.locales.indexOf(locale)!=-1){
state.locale=locale
}
},
设置季节(州、季节){
如果在您的Vue实例上(season>0&&season,您是否添加了Vuex商店
应该是这样的
import store from './store'
new Vue({
...
store,
...
})
nuxt中有两种存储模式,一种是经典模式,例如在store/index.js中手动创建存储
import Vuex from 'vuex'
const createStore = () => {
return new Vuex.Store({
state: {
counter: 0
},
mutations: {
increment (state) {
state.counter++
}
}
})
}
export default createStore
另一个是模块。您只需在store(store/index.js、store/something.js等)文件夹中创建包含状态、操作和变化的文件,例如
export const state = () => ({
counter: 0
})
export const mutations = {
increment (state) {
state.counter++
}
}
这两种方法都将允许您访问此项。$store inside mounted请提供更多信息和代码、在Vue实例上注册您的存储的代码以及设置season操作。问题已编辑)你真的需要手动初始化store吗?但是我应该如何初始化它?我使用的是Nuxt.js。有一个内置的全局$store对象。这是没有必要的。但是这两个模型之间的区别是什么?功能上的区别?还是什么?@AndreyF只是定义和结构的不同方法。