简体   繁体   English

商店/的Vuex Classic模式已弃用,并将在Nuxt 3中删除

[英]Vuex Classic mode for store/ is deprecated and will be removed in Nuxt 3

I have below files and could not find the reason for warning "Classic mode for store/ is deprecated and will be removed in Nuxt 3". 我有以下文件,找不到警告“存储/经典模式已弃用,并将在Nuxt 3中删除的经典模式”的原因。 Everything works fine just getting that annoying warning. 收到令人讨厌的警告,一切都很好。

modules/data.js file in store of nuxt.js. nuxt.js存储中的modules / data.js文件。

    const state = () => ({
      loadedPosts: []
    });

    const mutations = {
      setPosts(state, posts){
        state.loadedPosts = posts;
      }
    };

    const actions = {
      setPosts(vuexContext, posts){
        vuexContext.commit('setPosts', posts);
      }
    };

    const getters = {
      loadedPosts(state){
        return state.loadedPosts;
      }
    };

    export default {
      state,
      actions,
      getters,
      mutations
    };

index.js file in store of nuxt.js. nuxt.js存储中的index.js文件。

import Vuex from 'vuex';
import data from "~/store/modules/data";

const createStore = () => {
  return new Vuex.Store({
    modules: {
      data: {
        namespaced: true,
        ...data
      }
    }
  });
};

export default createStore;

Finally, after reading a lot of answers and blogs I figured out the solution. 最后,在阅读了很多答案和博客之后,我找到了解决方案。

Important:- Forgot what you know about vuex module in vue.js app. 重要说明:-忘记了您对vue.js应用程序中的vuex模块的了解。 Using Vuex in nuxt.js is a bit different. 在nuxt.js中使用Vuex有点不同。

Solution:- Nuxt.js lets you have a store directory with every file corresponding to a module. 解决方案:-通过 Nuxt.js,您可以拥有一个存储目录,其中每个文件都与一个模块相对应。 Just add necessary files in the store directly you don't need to create and add files to ' modules ' directory in store. 只需直接在商店中添加必要的文件,您无需创建文件并将其添加到商店中的' modules '目录。 index.js file is a special file and this is where we would put the logic that is not related to a module. index.js文件是一个特殊文件,这是我们放置与模块无关的逻辑的地方。

store/data.js 商店/data.js

export const state = () => ({
  loadedPosts: []
});

export const mutations = {
  setPosts(state, posts){
    state.loadedPosts = posts;
  }
};

export const actions = {
  setPosts(vuexContext, posts){
    vuexContext.commit('setPosts', posts);
  }
};

export const getters = {
  loadedPosts(state){
    return state.loadedPosts;
  }
};

Using state in project 在项目中使用状态

this.$store.data.loadedPosts

Using mutations in project 在项目中使用变异

this.$store.commit('data/setPosts', [{id: '1',...}, {id: '2',...}]);

Using actions in project 在项目中使用动作

this.$store.dispatch('data/setPosts', [{id: '1',...}, {id: '2',...}]);

Using getters in project 在项目中使用吸气剂

this.$store.getters['data/loadedPosts'];

Important References:- 重要参考:

  1. Watch this video Nuxt.js tutorial for beginners - nuxt.js vuex store 观看此视频Nuxt.js初学者教程-nuxt.js vuex商店
  2. Read this blog Efficiently understanding and using Nuxt + Vuex 阅读此博客有效了解和使用Nuxt + Vuex

Nuxt display this warning because you use a classic vuex store, not a « modules mode ». Nuxt显示此警告,因为您使用的是经典vuex存储,而不是“模块模式”。 You can learn more about in nuxt doc . 您可以在nuxt doc中了解更多信息。

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

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