counter.vue
1001 Bytes
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<template lang="html">
<div>
count: {{ count }} <br />
countAlias: {{ countAlias }} <br />
countPlusLocalState: {{countPlusLocalState}} <br />
localComputed: {{localComputed}}
</div>
</template>
<script>
import { mapState } from 'vuex'
export default {
data() {
return {
localCount: 10
}
},
computed: {
localComputed() {
return 'localComputed'
},
...mapState({
// 箭头函数可以让代码非常简洁
count: state => state.count,
// 传入字符串 'count' 等同于 `state => state.count`
countAlias: 'count',
// 想访问局部状态,就必须借助于一个普通函数,函数中使用 `this` 获取局部状态
countPlusLocalState(state) {
return state.count + this.localCount
}
})
},
mounted() {},
methods: {},
components: {}
}
</script>
<style lang="css">
</style>