您现在的位置是:首页 > 前端会客厅 > 前端框架前端框架

记录vue3不能正常获取vuex的问题

YU到边2022-09-02【前端框架】人已围观

简介原本store/index.js是这样写的,一直拿不到
const store = createStore({ state: { counter: 0 }, actions: {}, mutations: { }, modules: {

原本store/index.js是这样写的,一直拿不到
 
const store = createStore({
    state: {
        counter: 0
    },
    actions: {},
    mutations: {
    },
    modules: {
        menu
    }
});
export default {
    store
}


后来发现引入的方式不对,这样就ok了
 
export default createStore({
    state: {
        counter: 0
    },
    actions: {},
    mutations: {
    },
    modules: {
        menu
    }
});


vue3引入vuex的流程

1、vue -ui 引入xuex
2、src下面创建store - index.js
 
import {createStore} from 'vuex';
import menu from './moudles/menu'
// 通过mutations去改变vuex中的数据,对于异步的情况,通过actions提交mutations中的方法进而改变vuex中的数据

export default createStore({
    state: {
        counter: 0
    },
    actions: {},
    mutations: {
    },
    modules: {
        menu
    }
});


3、main.js
 
import store from "./store/";
app.use(store).mount('#app');


4、xxx.vue
 
import { useStore } from "vuex";
export default {
  setup() {
    const store = useStore();
    return{store}
  }
}

Tags:

很赞哦! ()

文章评论