File tree 3 files changed +48
-4
lines changed
3 files changed +48
-4
lines changed Original file line number Diff line number Diff line change 1
1
<template >
2
2
<div :class =" $style.hello" >
3
- <!-- <h1>{{ num }}</h1> -->
3
+ <h1 >{{ count }}</h1 >
4
+ <button @click =" add" >add</button >
4
5
</div >
5
6
</template >
6
7
7
8
<script >
8
-
9
9
import stark from ' ./stark.css'
10
10
export default {
11
11
name: ' HelloWorld' ,
12
+ computed: {
13
+ count () {
14
+ return this .$stark .state .count
15
+ },
16
+ },
12
17
created () {
13
- console .log (' stark' ,this .$stark .state )
18
+ console .log (' stark' , this .$stark .state )
19
+ },
20
+ methods: {
21
+ add (){
22
+ this .$stark .commit (' increment' )
23
+ }
14
24
}
15
25
}
16
26
</script >
Original file line number Diff line number Diff line change @@ -8,5 +8,11 @@ const debug = process.env.NODE_ENV !== 'production'
8
8
9
9
export default new Vuex . Stark ( {
10
10
state,
11
+ mutations : {
12
+ increment ( state ) {
13
+ state . count = state . count + 10
14
+ console . log ( '当前状态为' , state . count )
15
+ } ,
16
+ } ,
11
17
strict : debug ,
12
18
} )
Original file line number Diff line number Diff line change 1
1
import applyMixin from './mixin'
2
-
2
+ import Vue from 'vue'
3
3
export class Stark {
4
4
constructor ( options = { } ) {
5
5
console . log ( 'options' , options )
6
6
this . options = options
7
+ this . mutations = { }
8
+ const { commit } = this
9
+ this . commit = type => {
10
+ return commit . call ( this , type )
11
+ }
12
+ forEachValue ( options . mutations , ( mutationFn , mutationName ) => {
13
+ registerMutation ( this , mutationName , mutationFn )
14
+ } )
15
+ // 数据响应式
16
+ this . _vm = new Vue ( {
17
+ data : {
18
+ state : options . state ,
19
+ } ,
20
+ } )
7
21
}
8
22
get state ( ) {
9
23
return this . options . state
10
24
}
25
+
26
+ commit ( type ) {
27
+ this . mutations [ type ] ( )
28
+ }
29
+ }
30
+
31
+ function registerMutation ( store , mutationName , mutationFn ) {
32
+ store . mutations [ mutationName ] = ( ) => {
33
+ mutationFn . call ( store , store . state )
34
+ }
35
+ }
36
+
37
+ function forEachValue ( obj , fn ) {
38
+ Object . keys ( obj ) . forEach ( key => fn ( obj [ key ] , key ) )
11
39
}
12
40
13
41
export function install ( Vue ) {
You can’t perform that action at this time.
0 commit comments