File tree Expand file tree Collapse file tree 5 files changed +56
-14
lines changed Expand file tree Collapse file tree 5 files changed +56
-14
lines changed Original file line number Diff line number Diff line change 1
1
<template >
2
2
<div >
3
- Hello: {{user.firstName}} {{user.lastName }}
3
+ Hello: {{fullName }}
4
4
</div >
5
5
</template >
6
6
7
7
<script type="text/babel">
8
8
import Vue from ' vue'
9
- import { mapState } from ' vuex'
9
+ import { mapGetters } from ' vuex'
10
10
11
11
export default Vue .component (' app-header' , {
12
- computed: mapState ([
13
- ' user'
14
- ])
12
+ computed: {
13
+ ... mapGetters ([
14
+ ' fullName'
15
+ ])
16
+ }
15
17
})
16
18
</script >
Original file line number Diff line number Diff line change 1
1
<template >
2
- <nav >
2
+ <nav v-once >
3
3
<ul role =" menu" >
4
4
<li role =" presentation" >
5
5
<router-link to =" /" exact >home</router-link >
Original file line number Diff line number Diff line change 16
16
<script type="text/babel">
17
17
import appHeader from ' ../components/Header.vue'
18
18
import appNav from ' ../components/Nav.vue'
19
+ import { mapActions , mapState } from ' vuex'
19
20
20
21
export default {
21
22
components: {
22
23
appHeader,
23
24
appNav
25
+ },
26
+
27
+ methods: {
28
+ ... mapActions ([
29
+ ' getUser'
30
+ ])
31
+ },
32
+
33
+ created (){
34
+ this .getUser ()
24
35
}
25
36
}
26
37
</script >
Original file line number Diff line number Diff line change 2
2
<div >
3
3
<h2 >Edit user</h2 >
4
4
5
- <input v-model =" user .firstName" placeholder =" First name" />
6
- <input v-model =" user .lastName" placeholder =" Last name" />
7
- <input v-model =" user .age" type =" number" placeholder =" Age" />
5
+ <input v-model =" form .firstName" placeholder =" First name" />
6
+ <input v-model =" form .lastName" placeholder =" Last name" />
7
+ <input v-model =" form .age" type =" number" placeholder =" Age" />
8
8
9
- <select v-model =" user .gender" >
9
+ <select v-model =" form .gender" >
10
10
<option value =" male" >male</option >
11
11
<option value =" female" >female</option >
12
12
</select >
13
13
14
14
<button v-on:click =" save" >save</button >
15
+ <button v-on:click =" reset" >reset</button >
15
16
</div >
16
17
</template >
17
18
20
21
import { mapActions , mapState } from ' vuex'
21
22
22
23
export default {
24
+ data : () => {
25
+ return {
26
+ form: {
27
+ firstName: ' ' ,
28
+ lastName: ' ' ,
29
+ age: ' ' ,
30
+ gender: ' '
31
+ }
32
+ }
33
+ },
34
+
23
35
methods: {
24
36
... mapActions ([
25
- ' getUser' ,
26
37
' updateUser'
27
38
]),
28
39
29
40
save (){
30
- this .updateUser (this .user )
41
+ this .updateUser (this .form )
42
+ },
43
+
44
+ reset (){
45
+ this .update ()
46
+ },
47
+
48
+ update (){
49
+ this .form .firstName = this .user .firstName
50
+ this .form .lastName = this .user .lastName
51
+ this .form .age = this .user .age
52
+ this .form .gender = this .user .gender
31
53
}
32
54
},
33
55
34
56
mounted (){
35
- this .getUser ()
57
+ this .update ()
36
58
},
37
59
38
60
computed: mapState ([
Original file line number Diff line number Diff line change @@ -24,8 +24,15 @@ const mutations = {
24
24
}
25
25
}
26
26
27
+ const getters = {
28
+ fullName : state => {
29
+ return `${ state . firstName } ${ state . lastName } `
30
+ }
31
+ }
32
+
27
33
export default {
28
34
state,
29
35
mutations,
30
- actions
36
+ actions,
37
+ getters
31
38
}
You can’t perform that action at this time.
0 commit comments