@@ -22,12 +22,14 @@ This example has one form field bound to a Vue value called `name`. Here's the J
22
22
23
23
``` js
24
24
const app = new Vue ({
25
- el: ' #app' ,
25
+ el: ' #app' ,
26
26
data: {
27
- name: ' '
27
+ name: ' '
28
28
},
29
29
mounted () {
30
- if (localStorage .name ) this .name = localStorage .name ;
30
+ if (localStorage .name ) {
31
+ this .name = localStorage .name ;
32
+ }
31
33
},
32
34
watch: {
33
35
name (newName ) {
@@ -75,12 +77,16 @@ Now we've got two fields (again, bound to a Vue instance) but now there is the a
75
77
const app = new Vue ({
76
78
el: ' #app' ,
77
79
data: {
78
- name: ' ' ,
79
- age: 0
80
+ name: ' ' ,
81
+ age: 0
80
82
},
81
83
mounted () {
82
- if (localStorage .name ) this .name = localStorage .name ;
83
- if (localStorage .age ) this .age = localStorage .age ;
84
+ if (localStorage .name ) {
85
+ this .name = localStorage .name ;
86
+ }
87
+ if (localStorage .age ) {
88
+ this .age = localStorage .age ;
89
+ }
84
90
},
85
91
methods: {
86
92
persist () {
@@ -106,7 +112,7 @@ As mentioned above, Local Storage only works with simple values. To store more c
106
112
<h2 >Cats</h2 >
107
113
<div v-for =" (cat,n) in cats" >
108
114
<p >
109
- <span class =" cat" >{{cat}}</span > <button @click =" removeCat(n)" >Remove</button >
115
+ <span class =" cat" >{{ cat }}</span > <button @click =" removeCat(n)" >Remove</button >
110
116
</p >
111
117
</div >
112
118
@@ -122,14 +128,14 @@ This "app" consists of a simple list on top (with a button to remove a cat) and
122
128
123
129
``` js
124
130
const app = new Vue ({
125
- el: ' #app' ,
131
+ el: ' #app' ,
126
132
data: {
127
- cats: [],
128
- newCat: null
133
+ cats: [],
134
+ newCat: null
129
135
},
130
136
mounted () {
131
137
132
- if (localStorage .getItem (' cats' )) {
138
+ if (localStorage .getItem (' cats' )) {
133
139
try {
134
140
this .cats = JSON .parse (localStorage .getItem (' cats' ));
135
141
} catch (e) {
@@ -140,13 +146,16 @@ const app = new Vue({
140
146
methods: {
141
147
addCat () {
142
148
// ensure they actually typed something
143
- if (! this .newCat ) return ;
149
+ if (! this .newCat ) {
150
+ return ;
151
+ }
152
+
144
153
this .cats .push (this .newCat );
145
154
this .newCat = ' ' ;
146
155
this .saveCats ();
147
156
},
148
157
removeCat (x ) {
149
- this .cats .splice (x,1 );
158
+ this .cats .splice (x, 1 );
150
159
this .saveCats ();
151
160
},
152
161
saveCats () {
0 commit comments