uni 项目中如何使用 vuex

    技术2024-06-30  74

    说明

    uni 和 vue 几乎相同,所以操作也是一样的

    由于uni-app已经内置了vuex,所以只要正确引入就好了。

    新建 vuex 页面

    在根目录下新建 store 文件夹,文件夹内新建 index.js 文件

    书写 vuex 页面

    import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) const store = new Vuex.Store({ state: {}, mutations: {}, actions: {} }) export default store

    挂载 vuex

    在 main.js 文件中挂载 import store from './store' //引入vuex Vue.prototype.$store = store //把vuex定义成全局组件

    调用 vuex

    console.log(this.$store)
    Processed: 0.032, SQL: 9