...mapactions 和 ...mapgetters都是vuex提供的語法糖,在底層已經封裝好了,拿來就能用,簡化了很多操作。
其中...mapActions(['clickAFn'])
相當於this.$store.dispatch('clickAFn',{參數})
,mapActions中只需要指定方法名即可,參數省略。
...mapGetters(['resturantName'])
相當於this.$store.getters.resturantName
state (類似存儲全局變量的數據)
getters (提供用來獲取state數據的方法)
actions (提供跟后台接口打交道的方法,並調用mutations提供的方法)
mutations (提供存儲設置state數據的方法)
<script>
import {mapActions, mapGetters} from 'vuex'
export default {
name: 'A',
data () {
return {
}
},
methods:{
...mapActions( // 語法糖
['modifyAName'] // 相當於this.$store.dispatch('modifyName'),提交這個方法
),
trunToB () {
this.$router.push({path: '/componentsB'}) // 路由跳轉到B
}
},
computed: {
...mapGetters(['resturantName']) // 動態計算屬性,相當於this.$store.getters.resturantName
}
}
</script>