【vuex】的用法(語法糖)


...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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM