剩余运算符简介:
let [a, ...b] = [1, 2, 3];
//a = 1
//b = [2, 3]
与vuex的mapState、mapmutation等方法的结合
import { mapState, mapMutations } from 'vuex'
computed: {
...mapState({
currientcity: 'city'
})
},
methods: {
handleclick (city) {
this.changecity(city)
this.$router.push('/')
},
...mapMutations(['changecity'])
},
在computed属性中利用剩余运算符就是将vuex中的state.city的值赋给了currientcity,之前要用state存储的数据要使用this.$store.state.city获取,现在只需要使用this.currientcity就可以获取。
在methods中利用展开运算符,将changcity函数赋值,就可以通过this.changecity获取mutations中的函数。未引入前要通过this.store.commit(‘changecity’,city)才可以获取,是不是简便了很多。