Vuex mapGetters混入原理简单实现
# Vuex mapGetters混入原理简单实现
const getters = {
a: () => 1,
b: () => 2,
c: () => 3,
};
function add(key) {
let obj = {};
key.forEach((keys) => {
if (!obj.hasOwnProperty(keys)) {
obj[keys] = getters[keys];
}
});
return obj;
}
export default {
computed: {
// ...mapGetters(["test"]),
...add(["a", "b"]),
},
mounted() {
console.log(this.a);
},
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24