如何在uni-app使用vuex如何将vuex模块化!

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

1、在项目的根目录下,创建一个名为store的文件夹然后在该文件夹下创建一个index.js的js文件
2、在该js文件下定义公共的数据以及方法函数,并且把它导出

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

3、在入口文件即:main.js挂载vuex

import Vue from 'vue'
import App from './App'
//引入vuex
import store from './store'
//把vuex定义成全局组件
Vue.prototype.$store = store
Vue.config.productionTip = false

App.mpType = 'app'

const app = new Vue({
    ...App,
//挂载
    store
})
app.$mount()

4、在单页面里使用vuex

<script>
    export default {
        created () {
            console.log(this.$store)
        }
    }
</script>

二、如何将vuex模块化

目录结构为
|—demo1.js
|—demo2.js
index.js

index.js

import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
const context = require.context('./modules', false, /\.js$/);
const moduleStores = {};

context.keys().forEach(key => {
    // 获取读取到的文件名字并且截取
    const fileName = key.slice(2, -3);
    //通过 context(key)导出文件内容
    const fileModule = context(key).default;
    moduleStores[fileName] = {
        ...fileModule,
    };
});
export default new Vuex.Store({
    modules: {
        ...moduleStores,
    },
});

demo1.js

const demoname = {
    fn: 'fn',
}
const store = {
    state: {
        demo: '名字'
    },
    getters: {
        getLocale: state => {
            return state.demo + 321
        }
    },
    mutations: {
        [demoname.fn]: (state, localeVal) => {
            console.log('被执行了', localeVal)
        }
    },
    actions: {
        demoactions: ({ commit }, localeVal) => {
            commit(demoname.fn, localeVal)
        }
    }
}
export default store

demo2.js同上

页面使用

<template>
    <view class="content">
        <view class="text-area">
            <view class="title">{{demo1.demo}}</view>
            <view class="title">{{getLocale}}</view>
        </view>
    </view>
</template>

<script>
    import { mapState, mapGetters, mapActions, mapMutations } from 'vuex'
    export default {
        data() {
            return {
                title: 'Hello'
            }
        },
        computed: {
            ...mapGetters(['getLocale']),
            ...mapState([
                'demo1'
            ])
        },
        onLoad() {
            this.fn('传递的内容')
            this.demoactions('这是一个异步')
        },
        methods: {
            ...mapMutations([
                'fn'
            ]),
            ...mapActions([
                'demoactions'
            ])
        }
    }
</script>

<style>
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

淡忘_cx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值