若有多个getter时,可用...mapGetters([ ]),需要先export引入,且是es6语法,需要安装bable插件
...mapActions([ ])同理;
这样写可以简化代码;
具体用法如下:
// productListOne.vue中
<script>
import {mapGetters} from 'vuex'
import {mapActions} from 'vuex'
export default {
// 通过调用方法获取store.js里的数据
computed: {
products() {
return this.$store.state.products
},
// saleProducts() {
// return this.$store.getters.saleProducts;
// }
// 若有多个getter时,可用...mapGetters([]),需要先export引入,且是es6语法
...mapGetters([
"saleProducts",
// "akjsv",
// "akjsv",
])
},
methods: {
// reducePrice: function(amount){
// // this.$store.state.products.forEach(product => {
// // product.price -= 1;
// // });
// // this.$store.commit('reducePrice')
// this.$store.dispatch("reducePrice",amount) // 要想走action里的方法,此刻就需要dispatch
// }
// 若有多个action时,可用...mapActions([])
...mapActions([
"reducePrice",
// "kgfjag",
// "kgfjag",
])
}
}
</script>