Vue中两种方式使用store的state和action

2019-03-21  本文已影响0人  leslie1943

更推荐读一下这篇文章(更简洁,更容易移植!)

🚀 https://www.jianshu.com/p/7a0e1330faac 🚀

import { mapState ,mapActions} from 'vuex'
export default {
    methods:{
        // 格式:mapActions(namespace,[actionName])
        ...mapActions('home', ['getHomeData']),
        ...mapActions('dictionary', ['getSubCertType']),
    },
    computed: {
        // 方式一: 格式: stateName: state => state.namespace.stateName,
        ...mapState({
            homeData: state => state.home.homeData,
            subCertType: state => state.dictionary.subCertType
        }),
        // 方式二: 格式: ...mapState(namespace,{ stateName : state => state.stateName}),
        ...mapState('home',{ homeData : state => state.homeData}),
        ...mapState('dictionary',{ subCertType : state => state.subCertType})
    }
}
上一篇 下一篇

猜你喜欢

热点阅读