vuex example01

5 篇文章 0 订阅

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title> x</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.22/dist/vue.js"></script> 
<link href="https://cdn.jsdelivr.net/npm/animate.css@3.5.1" rel="stylesheet" type="text/css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/velocity/1.2.3/velocity.min.js"></script>
<!--
<script   src="C:\Users\SDSC-XAD\node_modules\vuex\dist\vuex.js" />
<script   src="https://unpkg.com/vuex@2.3.0" ></script>
-->
<script   src="https://unpkg.com/vuex@2.3.0" ></script>

</head>
<body>
    <h1>vuex</h1>
    
     <h2>  vote example</h2>
    <hr>
  
    
    <div id="example-2">
    <div v-for="(item, key, index) in this.$store.state.voteitems">
    <!-- // right
    <label >{{item}},{{item.text}},{{item.value}}</label>
    -->
     <abc v-bind:item="item" v-bind:key="item.id"></abc>

      
     </div>
    </div>
    
<script type="text/javascript"> 
 var myStore =  new Vuex.Store({
        state:{
            //存放组件之间共享的数据
            voteitems:[{text:'A',value:0},{text:'B',value:0},{text:'C',value:0},{text:'D',value:0}],
            
        },
         mutations:{
             //显式的更改state里的数据
             changeadd:function(state,a){
              
              var t=myStore.state.voteitems
              for(var i=0;i<t.length;i++)
              {
                    if(t[i].text==a){
                       t[i].value++
                    }
              }
                         
             },
              changedet:function(state,a){
              
              var t=myStore.state.voteitems
              for(var i=0;i<t.length;i++)
              {
                    if(t[i].text==a){
                       t[i].value--
                    }
              }
                         
             },
             
             changeadjust:function(state,option){
              var a=option.targ
              var type=""
             type= option.type
              var t=myStore.state.voteitems
              for(var i=0;i<t.length;i++)
              {
                    if(t[i].text==a){
                       if(type=='add'){
                       t[i].value++
                       }else{
                       t[i].value--
                       }
                       
                    }
              }
                         
             },
             
              
         },
         getters:{
           
         },
         actions:{
           
         }
    });
      
 Vue.component('abc',{
 
        template:`
            <div>
                <label :name="item.text">{{item.text}} {{item.value}}</label>
                <button @click="changeCount('add',$event)" :id="item.text" >add</button>
                <button @click="changeCount('det',$event)" :id="item.text" >det</button>
            </div>
        `,
        methods:{
                changeCount: function(type,event){   
                
                /*  ///注释,测试多参数传递
                if(type=='add'){
                    var targ=event.srcElement.getAttribute("id")
                    this.$store.commit('changeadd',targ)
                }else{
                    var targ=event.srcElement.getAttribute("id")
                    this.$store.commit('changedet',targ)
                }
                */
                
                //测试多参数传递  必须写成对象或者数组再传入
                var targ=event.srcElement.getAttribute("id")
                var option={targ,type}
                    this.$store.commit('changeadjust',option)        
                
            },
        },
        
        computed: {
                abcname:function(){
                    //return this.$myStore.state.name
                    //return this.$store.state.name
                },
            },
        props:["item"]    
            
        })
        
     
     new Vue({
          el: '#example-2',
          data: {
                 
                   //voteitems:[{text:'A',value:0},{text:'B',value:0},{text:'C',value:0},{text:'D',value:0}],
                  // voteitems: this.$store.state.voteitems 
          },
         store:myStore,
           
          computed:{
          
             // voteitemss(){
            //        this.voteitems = this.$store.state.voteitems
             // }
          }   
          
        })
         
    </script>
    
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值