mapState和mapGetters

就是一个代码生成方法

使用方法

1.引入mapState和mapGetters

1
2
import { mapState } from 'vuex';
import { mapGetters } from "vuex";

2.在computed属性中调用mapState方法和mapGetters方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<script>
import { mapState } from 'vuex';
import { mapGetters } from "vuex";

export default {
name: 'count',
computed: {
...mapState(["sum", "school", "subject"]),
// 对象写法
// ...mapState({
// sum: state => state.sum,
// school: state => state.school,
// subject: state => state.subject
// }),
// ...mapGetters({
// calculate: getters=> getters.calculate
// }),

// 数组写法(简写,属性名与读取名相同)
...mapState(["sum", "school", "subject"]),
...mapGetters(['calculate']),
}
};
</script>