博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
vue2
阅读量:6936 次
发布时间:2019-06-27

本文共 2751 字,大约阅读时间需要 9 分钟。

src    main.js    App.vue    store        actions.js        actions.js        index.js        mutations.js        types.js

main.js

1
2
3
4
5
6
7
8
9
10
import 
Vue from 
'vue'
import 
App from 
'./App.vue'
 
import 
store from 
'./store/'
 
new 
Vue({
    
store,
    
el: 
'#app'
,
    
render: h => h(App)
})

 App.vue

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
<
template
>
  
<
div 
id="app">
    
<
h3
>welcome vuex-demo</
h3
>
    
<
input 
type="button" value="增加" @click="increment">
    
<
input 
type="button" value="减少" @click="decrement">
    
<
input 
type="button" value="偶数才能点击+" @click="clickOdd">
    
<
input 
type="button" value="点击异步" @click="clickAsync">
 
    
<
div
>
      
现在数字为: {
{count}}, 它现在是 {
{getOdd}}
    
</
div
>
  
</
div
>
</
template
>
 
<
script
>
  
import {mapGetters, mapActions} from 'vuex'
 
  
export default{
    
computed:mapGetters([
      
'count',
      
'getOdd'
    
]),
    
methods:mapActions([
      
'increment',
      
'decrement',
      
'clickOdd',
      
'clickAsync'
    
])
  
}
</
script
>
 
<
style
>
#app {
  
font-family: 'Avenir', Helvetica, Arial, sans-serif;
  
-webkit-font-smoothing: antialiased;
  
-moz-osx-font-smoothing: grayscale;
  
text-align: center;
  
color: #2c3e50;
  
margin-top: 60px;
}
 
h1, h2 {
  
font-weight: normal;
}
 
ul {
  
list-style-type: none;
  
padding: 0;
}
 
li {
  
display: inline-block;
  
margin: 0 10px;
}
 
a {
  
color: #42b983;
}
</
style
>

 action.js

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
import 
* as types from 
'./types'
 
export 
default 
{
    
increment: ({
        
commit
    
}) => {
        
commit(types.INCREMENT);
    
},
    
decrement: ({
        
commit
    
}) => {
        
commit(types.DECREMENT);
    
},
    
clickOdd: ({
        
commit,
        
state
    
}) => {
        
if 
(state.mutations.count % 2 == 0) {
            
commit(types.INCREMENT);
        
}
    
},
    
clickAsync: ({
        
commit
    
}) => {
        
new 
Promise((resolve) => {
            
setTimeout(
function
() {
                
commit(types.INCREMENT);
            
}, 1000);
        
})
    
}
}

 getters.js

1
2
3
4
5
6
7
8
export 
default 
{
    
count: (state) => {
        
return 
state.count;
    
},
    
getOdd: (state) => {
        
return 
state.count % 2 == 0 ? 
'偶数' 
'奇数'
    
}
}

 index.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import 
Vue from 
'vue'
import 
Vuex from 
'vuex'
 
Vue.use(Vuex);
 
import 
mutations from 
'./mutations'
import 
actions from 
'./actions'
 
 
export 
default 
new 
Vuex.Store({
    
modules:{
        
mutations
    
},
    
actions
});

 mutation.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import 
{
    
INCREMENT,
    
DECREMENT
} from 
'./types'
import 
getters from 
'./getters'
 
const state = {
    
count: 20
};
 
const mutations = {
    
[INCREMENT](state) {
        
state.count++;
    
},
    
[DECREMENT](state) {
        
state.count--;
    
}
};
 
export 
default 
{
    
state,
    
mutations,
    
getters
}

 types.js

1
2
3
export 
const INCREMENT = 
'INCREMENT'
;
 
export 
const DECREMENT = 
'DECREMENT'
;

 

本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/7072363.html,如需转载请自行联系原作者

你可能感兴趣的文章
WordPress开发之WP Custom Register Login插件试用
查看>>
微信小程序原生组件swiper在mpvue工程中使用注意事项
查看>>
Pav Metro Store OpenCart 自适应主题模板 ABC-0215
查看>>
如何初始化类的static成员变量?
查看>>
vue自定义指令
查看>>
MD5 AES Des 加密解密
查看>>
sc config 命令(详细)
查看>>
mongo c#驱动介绍操作
查看>>
ping: icmp open socket: Operation not permitted 的解决办法
查看>>
清除error.log、access.log并限制Apache日志文件大小的方法
查看>>
我的友情链接
查看>>
【Java基础】Java常见的异常
查看>>
apache忽略文件后缀
查看>>
教你如何rEFIt-让你开机免按option!
查看>>
linux 权限管理
查看>>
Windows 2003 标准版_企业版_SP1_SP2_R2的区别
查看>>
AD域管理系列(6)-- 常见处理
查看>>
rpm包安装apache发布多个虚拟主机
查看>>
运维工具
查看>>
喜欢与需要
查看>>