mapActions() 返回的是一個對象, 用了 ... 擴展符后,才可以放進一個對象里,和其他組件內定義的 method 在同一個 methods 對象。
?
?
{
methods: mapActions() // 如果沒有其它組件內的定義的方法,可以這樣寫
}
{
methods: {
...mapActions(),// 如果有其他定義的方法
otherMethod1 () {},
otherMethod2 () {}
}
?
?
?
}
假設mapActions(),返回的是
{a() {},b() {}
}
那 ...mapActions(),只不過是把a,b都拿出來跟其他方法放在一起了而已。
...代表兩種意思,一種是剩余操作符,一種是擴展運算符,你題目里用的那個應該是剩余操作的意思,而...mapActions才是擴展運算符。
?
?
其中參數的使用方法原理為::
?
?
methods: {'some/nested/module/foo': (val) {return this.$store.dispatch('some/nested/module/foo', val)) } }
但這個Mutations這么長, 一般不會這樣去轉換,會加個別名
methods: {...mapActions({foo: 'some/nested/module/foo',bar: 'some/nested/module/bar'})
}//相當于下面的寫法methods: {foo(val){return this.$store.dispatch('some/nested/module/foo', val)) } //bar 省略.... }) }