Toggle navigation
Toggle navigation
This project
Loading...
Sign in
Hooke
/
demo-vue-vuex
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
Hooke
2016-10-31 16:14:55 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
e423540697e146bf315ba74397bf726671875fe7
e4235406
1 parent
7623baf2
no message
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
106 additions
and
3 deletions
post-reading.md
src/components/counter.vue
post-reading.md
0 → 100644
View file @
e423540
#### Distinguish helper
###### *mapState*
> 读取state中的值进行操作, 不对 state本身进行计算.
```
javascript
// 单独计算
computed
:
mapState
({
count
:
state
=>
state
.
count
,
countAlias
:
'count'
,
// 别名
countPlusLocalState
(
state
)
{
return
state
.
count
+
this
.
localCount
// 结合本地值进行计算
}
})
// 计算属性名称 === 状态子树名称
computed
:
mapState
([
'count'
])
// 混合计算
computed
:
{
localComputed
()
{
/* ... */
},
...
mapState
({
count
:
state
=>
state
.
count
,
countAlias
:
'count'
,
// 别名
countPlusLocalState
(
state
)
{
return
state
.
count
+
this
.
localCount
// 结合本地值进行计算
}
})
}
```
###### mapGetters
> 混合计算时:使用*对象扩展操作符*, 名称相同时传入同名数组, 映射不同名称时, 传入对象键值对, 键值对可以操作参考上面示例混合计算.
```
javascript
// 混合计算
computed
:
{
localComputed
()
{
/* ... */
},
...
mapGetters
([
'doneTodosCount'
,
'anotherGetter'
])
}
// 映射不同名称
mapGetters
({
doneCount
:
'doneTodosCount'
})
```
##### Mutations
###### Payload
```
javascript
mutations
:
{
increment
(
state
,
payload
)
{
state
.
count
+=
payload
.
amount
}
}
```
```
javascript
//
store
.
commit
(
'increment'
,
{
amount
:
10
})
// 对象风格
store
.
commit
({
type
:
'increment'
,
amount
:
10
})
```
###### mapMutations
```
javascript
methods
:
{
...
mapMutations
([
'increment'
// 映射 this.increment 到 this.$store.commit('increment')
]),
...
mapMutations
({
add
:
'increment'
// 映射 this.add 到 this.$store.commit('increment')
})
}
```
###### Const Named Mutation
```
javascript
// mutation-types.js
export
const
SOME_MUTATION
=
'SOME_MUTATION'
```
```
javascript
// store.js
import
{
SOME_MUTATION
}
from
'./mutation-types'
mutations
:
{
[
SOME_MUTATION
]
(
state
)
{
// change state
}
}
```
src/components/counter.vue
View file @
e423540
...
...
@@ -8,9 +8,7 @@
</template>
<script>
import {
mapState
} from 'vuex'
import { mapState } from 'vuex'
export default {
data() {
...
...
Please
register
or
login
to post a comment