简体   繁体   English

无法从Mounted()钩子Nuxt.js中的Vuex存储分派方法

[英]Can't dispatch method from Vuex store in mounted() hook Nuxt.js

I have a problem with dispatching the method from the Vuex store in mounted() hood? 我在Mounted()引擎罩中从Vuex存储调度方法时遇到问题吗? This's an error that appeared. 这是出现的错误。

在此处输入图片说明

I'm just doing this in mounted hook: this.$store.dispatch('setSeason', seasonValue); 我只是在挂接钩中执行此操作: this.$store.dispatch('setSeason', seasonValue);

I saw how to initialize store in official documentation, but it doesn't work... 我在官方文档中看到了如何初始化存储,但是不起作用...

This is my code for Vuex store initialization: 这是我用于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;

On your Vue instance, have you added the Vuex store? 在您的Vue实例上,您是否已添加Vuex存储?

It should look something like this 它应该看起来像这样

import store from './store'

new Vue({
  ...
  store,
  ...  
})

There two modes of the store in nuxt, one is classical eg you manually create store in store/index.js 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

Other is modules. 其他是模块。 You just create files inside store ( store/index.js, store/something.js etc) folder with state, actions and mutations eg 您只需在store(store / index.js,store / something.js等)文件夹中创建带有状态,操作和突变的文件,例如

export const state = () => ({
  counter: 0
})

export const mutations = {
  increment (state) {
    state.counter++
  }
}

https://nuxtjs.org/guide/vuex-store/ https://nuxtjs.org/guide/vuex-store/

Both of this method will allow you to access this.$store inside mounted 这两种方法都将允许您访问this。$ store

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM