-
Notifications
You must be signed in to change notification settings - Fork 3
/
vuejs3.txt
589 lines (416 loc) · 10.5 KB
/
vuejs3.txt
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
>>>>>>>> Vue 3 <<<<<<<<<
--------------------------------------
Keys:
vue3 js:
submit form, validation, props, router
store, actions, mutations, getters
--------------------------------------
Mirroring started
--------------------------------------
--------------------------------------
//Component.vue
<template>
</template>
<script>
export default {
}
</script>
<style>
</style>
---------------------------------------
---------------------------------------
Getting Started:
Install:
npm install -g @vue/cli
# OR
yarn global add @vue/cli
Create a project:
vue create my-project
# OR
vue ui
---------------------------------------
---------------------------------------
>>>> Vue 3 Vuex Store <<<<<<<
Website:
https://learnvue.co/tutorials/vuex-in-vue-3
npm install vuex@next
--> main.js file
import { createApp } from "vue";
import { createStore } from "vuex";
// Create a new store instance or import from module.
const store = createStore({
/* state, actions, mutations */
});
const app = createApp();
app.use(store);
app.mount("#app");
--> Vuex State
import { createApp } from "vue";
import { createStore } from "vuex";
const store = createStore({
state: {
count: 0,
},
});
const app = createApp();
app.use(store);
app.mount("#app");
--> Access Vuex State - Options API
<script>
export default {
mounted() {
console.log(this.$store.state.count) // this.$store
},
}
</script>
<template>{{ count }}</template>
<script>
export default {
computed: {
count() {
return this.$store.state.count
},
},
}
</script>
--> Using Vuex mapState
<script>
import { mapState } from 'vuex'
export default {
computed: mapState({
count: 'count',
// OR
count: (state) => state.count,
// OR IF WE NEED ACCESS TO `this`
countPlusMultiplier(state) {
return state.count + this.multiple
},
}),
}
</script>
--> Access Vuex State - Composition API
<script setup>
import { computed } from 'vue'
import { useStore } from 'vuex'
const store = useStore()
const count = computed(() => store.state.count)
</script>
<template>{{ count }}</template>
--> Using Vuex Helpers in the Composition API
npm install vuex-composition-helpers@next
<script setup>
import { useState, useActions } from 'vuex-composition-helpers/dist'
const { count } = useState(['count'])
</script>
<template>{{ count }}</template>
==>> ( Vuex Mutations )
const store = createStore({
state: {
count: "",
},
mutations: {
INCREMENT_COUNT(state, payload) {
state.count += payload;
},
},
});
-->
// Option 1
store.commit('INCREMENT_COUNT', 5)
// Option 2
store.commit({
type: 'INCREMENT_COUNT',
amount: 5,
})
-->
<template>
{{ count }}
<button @click="store.commit('INCREMENT_COUNT', 1)">Increment</button>
</template>
---> Mapping Vuex Mutations - Options API
<template>
{{ count }}
<button @click="INCREMENT_COUNT(1)">Increment</button>
</template>
<script>
import { mapMutations } from 'vuex'
export default {
computed: {
count() {
return this.$store.state.count
},
},
methods: mapMutations(['INCREMENT_COUNT']),
}
</script>
--> Mapping Vuex Mutations - Composition API
<script setup>
import { useState, useMutations } from 'vuex-composition-helpers/dist'
const { count } = useState(['count'])
const { INCREMENT_COUNT } = useMutations(['INCREMENT_COUNT'])
console.log(useMutations)
</script>
<template>
{{ count }} <button @click="INCREMENT_COUNT(1)">Increment</button>
</template>
===> ( Vuex Actions )
const store = createStore({
state: {
count: 0,
},
mutations: {
INCREMENT_COUNT(state, payload) {
state.count += payload;
},
},
actions: {
incrementCount(context, payload) {
context.commit("INCREMENT_COUNT", payload);
},
},
});
-->
const store = createStore({
// ...
actions: {
incrementCount(context, payload) {
setTimeout(() => {
context.commit("INCREMENT_COUNT", payload);
}, 1000);
},
},
// ...
});
==> ( Mapping Vuex Actions )
<template>
{{ count }}
<button @click="incrementCount(1)">Increment</button>
</template>
<script>
import { mapActions } from 'vuex'
export default {
computed: {
count() {
return this.$store.state.count
},
},
methods: mapActions(['incrementCount']),
}
</script>
<script setup>
import { useState, useActions } from 'vuex-composition-helpers/dist'
const { count } = useState(['count'])
const { incrementCount } = useActions(['incrementCount'])
</script>
<template>
{{ count }} <button @click="incrementCount(1)">Increment</button>
</template>
==> ( Gatters )
<script>
export default {
computed: {
doubleCountPlusOne() {
return this.$store.getters.doubleCountPlusOne
},
},
methods: mapActions(['incrementCount']),
}
</script>
const store = createStore({
getters: {
countOverValue: (state) => (val) => {
return state.count > val;
}
});
--> Mapping our Vuex Getters
<template>
{{ doubleCountPlusOne }}
<button @click="incrementCount(1)">Increment</button>
</template>
<script>
import { mapActions, mapGetters } from 'vuex'
export default {
computed: mapGetters(['doubleCountPlusOne']),
methods: mapActions(['incrementCount']),
}
</script>
-->
<script setup>
import { useActions, useGetters } from 'vuex-composition-helpers/dist'
const { doubleCountPlusOne } = useGetters(['doubleCountPlusOne'])
const { incrementCount } = useActions(['incrementCount'])
</script>
<template>
{{ doubleCountPlusOne }}
<button @click="incrementCount(1)">Increment</button>
</template>
----------> End Of Store <--------------
----------------------------------------
----------------------------------------
--------> Vuex Store <------------------
Start Store:
Installation and Setup:
In order to get started with Vuex, you can install it with npm or yarn.
npm install vuex@4 --save
# or with yarn
yarn add vuex@4
Then instantiate it via a createStore() function much like Vue 3's createApp() function.
// store/index.js
import {createStore} from 'vuex'
export default createStore()
Lastly, you register it with Vue like any other Vue plugin with the use() method.
// main.js
import { createApp } from 'vue'
import store from '@/store' // short for @/store/index
const app = createApp({ /* your root component */ })
app.use(store)
Store Definition
Stores in Vuex are defined via an object passed to the createStore function. The object can have any of the following properties: state, getters, mutations, and actions.
// store/index.js
export default createStore({
state:{},
getters:{},
mutations: {},
actions:{}
})
//State:
state:{
user: { name: 'John Doe', email: 'fake@email.com', username: 'jd123'},
posts: [],
someString: 'etc'
}
// ProfileComponent.vue
<template>
<h1>Hello, my name is {{$store.state.user.name}}</h1>
</template>
Or we can clean up the template a bit by using a computed property.
// ProfileComponent.vue
<template>
<h1>Hello, my name is {{name}}</h1>
</template>
<script>
export default{
computed:{
name(){ return this.$store.user.name }
}
}
</script>
<template>
<h1>Hello, my name is {{user.name}}</h1>
</template>
<script>
export default{
computed:{
...mapState(['user'])
}
}
</script>
//Gatters
{
state:{
posts: ['post 1', 'post 2', 'post 3', 'post 4']
},
// the result from all the postsCount getters below is exactly the same
// personal preference dicates how you'd like to write them
getters:{
// arrow function
postsCount: state => state.posts.length,
// traditional function
postsCount: function(state){
return state.posts.length
},
// method shorthand
postsCount(state){
return state.posts.length
},
// can access other getters
postsCountMessage: (state, getters) => ${getters.postsCount} posts available
}
}
Accessing the store's getters is much the same as accessing the state except you look under the getters property instead of the state property.
// FeedComponent.vue
<template>
<p>{{$store.getters.postsCount}} posts available</p>
</template>
You could also use a computed property in your component or a helper function (this time mapGetters) like with the state.
// FeedComponent.vue
<template>
<p>{{postsCount}} posts available</p>
</template>
<script>
import {mapGetters} from 'vuex'
export default{
computed:{
...mapGetters(['postsCount'])
}
}
</script>
{
state: {
posts: ['post 1', 'post 2', 'post 3', 'post 4'],
user: { postsCount: 2 }
errors: []
}
mutations:{
// convention to uppercase mutation names
INSERT_POST(state, post){
state.posts.push(post)
},
INSERT_ERROR(state, error){
state.errors.push(error)
},
INCREMENT_USER_POSTS_COUNT(state, error){
state.user.postsCount++
}
},
actions:{
async insertPost({commit}, payload){
//make some kind of ajax request
try{
await doAjaxRequest(payload)
// can commit multiple mutations in an action
commit('INSERT_POST', payload)
commit('INCREMENT_USER_POSTS_COUNT')
}catch(error){
commit('INSERT_ERROR', error)
}
}
}
}
// PostEditorComponent.vue
<template>
<input type="text" v-model="post" />
<button @click="$store.dispatch('insertPost', post)">Save</button>
</template>
// PostEditorComponent.vue
<template>
<input type="text" v-model="post" />
<button @click="savePost">Save</button>
</template>
<script>
export default{
methods:{
savePost(){
this.$store.dispatch('insertPost', this.post)
}
}
}
</script>
// PostEditorComponent.vue
<template>
<input type="text" v-model="post" />
<button @click="insertPost(post)">Save</button>
</template>
<script>
import {mapActions} from 'vuex'
export default{
methods:{
...mapActions(['insertPost'])
}
}
</script>
Website:
https://vueschool.io/articles/vuejs-tutorials/vuex-the-official-vuejs-store/
End store :
----------------------------------------
----------------------------------------