doubleyong
管理员
管理员
  • 最后登录2025-12-02
  • 发帖数1198
  • 最爱沙发
  • 喜欢达人
  • 原创写手
  • 社区居民
  • 忠实会员
阅读:8304回复:0

[vue]mapGetters , mapActions 在使用时,还有其它方法时,如何书写

楼主#
更多 发布于:2017-12-07 10:53
在使用VUEX中使用到了mapGettersmapActions.

1. mapGetters或mapActions的使用方法


调用其方法时,使用如下代码:

      computed:mapGetters(['count'])

如要要调用mapGettermapActions中的多个方法:

      computed:mapGetters(['count','id','username'])

如果想给getter属性换名字

     computed:mapGetters({doneCount:'count'})

2. mapGetters或mapActions使用原理
如以下代码:


computed:mapGetters(['count'])

   实际上等价于
      computed: {      
           count() {
                   return this.$store.state.count
             }
    }

    mapGetters (): 返回的是一个对象,这个对象里存储的是对应的函数

3. computed如何还有其它的计算,那应该如何写

computed: {
        localComputed () { /* ... */ },
       // 使用对象展开运算符将此对象混入到外部对象中
        ...mapGetters({
           // ...
        })
}

注:实现方法就是在方法前加三个点...mapGetter, 这三个点代表什么意思呢,它是es6的语法,代码是将此对象合并到,外部对象中。数组中也可以这样使用。关于对运算符的,详细介绍,可参考:http://bugshouji.com/sharetel/t341-1


参考:http://www.jb51.net/article/111589.htm      
           https://segmentfault.com/q/1010000010347791
           http://blog.csdn.net/qq_34629352/article/details/78155803
知识需要管理,知识需要分享
游客


返回顶部

公众号

公众号