不少時候 , $store.state.dialog.show
、$store.dispatch('switch_dialog')
這種寫法又長又臭 , 很不方便 , 咱們沒使用 vuex 的時候 , 獲取一個狀態只須要 this.show
, 執行一個方法只須要 this.switch_dialog
就好了 , 使用 vuex 使寫法變複雜了 ?vue
使用 mapState、mapGetters、mapActions
就不會這麼複雜了。vuex
以 mapState 爲例 :this
<template> <el-dialog :visible.sync="show"></el-dialog> </template> <script> import {mapState} from 'vuex'; export default { computed:{ //這裏的三點叫作 : 擴展運算符 ...mapState({ show:state=>state.dialog.show }), } } </script>
至關於 :spa
<template> <el-dialog :visible.sync="show"></el-dialog> </template> <script> import {mapState} from 'vuex'; export default { computed:{ show(){ return this.$store.state.dialog.show; } } } </script>
mapGetters、mapActions 和 mapState 相似 , mapGetters
通常也寫在 computed
中 , mapActions
通常寫在 methods
中。code