zoukankan      html  css  js  c++  java
  • vuex

    vuex相当于java中的一个公共的父类,里面有state(存放公共的属性)

    基本语法:

     Vue.use(Vuex);//在创建Vue实例之前
       var myStore =  new Vuex.Store({
            state:{
                //存放组件之间共享的数据
                name:"jjk"
            },
             mutations:{
                 //显式的更改state里的数据
             },
             getters:{
                 //获取数据的方法
             },
             actions:{
                 //
             }
        });
        new Vue({
            el:"#app",
            data:{
                name:"dk"
            },
            store:myStore,
            mounted:function(){
                console.log(this)//控制台
            }
        })


    ----
     Vue.use(Vuex);
       var myStore =  new Vuex.Store({
            state:{
                //存放组件之间共享的数据
                name:"jjk"
            },
             mutations:{
                 //显式的更改state里的数据
             },
             getters:{
                 //过滤state数据
             },
             actions:{
                 //
             }
        });
    
        Vue.component('hello',{
            template:"<p>{{name}}</p>",
            computed: {
                name:function(){
                    return this.$store.state.name
                }
            },
             mounted:function(){
                console.log(this)
            }
        })
        new Vue({
            el:"#app",
            data:{
                name:"dk"
            },
            store:myStore,
            mounted:function(){
                console.log(this)
            }
        })
    输出jjk

    -----
     Vue.use(Vuex);
       var myStore =  new Vuex.Store({
            state:{
                //存放组件之间共享的数据
                name:"jjk",
                age:18
            },
             mutations:{
                 //显式的更改state里的数据
             },
             getters:{
                 getAge:function(state){
                     return state.age;
                 }
             },
             actions:{
                 //
             }
        });
    
        Vue.component('hello',{
            template:"<p>姓名:{{name}} 年龄:{{age}}</p>",
            computed: {
                name:function(){
                    return this.$store.state.name
                },
                age:function(){
                    return this.$store.getters.getAge
                }
            },
             mounted:function(){
                console.log(this)
            }
        })
        new Vue({
            el:"#app",
            data:{
                name:"dk"
            },
            store:myStore,
            mounted:function(){
                console.log(this)
            }
        })
    输出 姓名:jjk 年龄:18



    ----
    Vue.use(Vuex);
       var myStore =  new Vuex.Store({
            state:{
                //存放组件之间共享的数据
                name:"jjk",
                age:18,
                num:1
            },
             mutations:{
                 //显式的更改state里的数据
                 change:function(state,a){
                    //  state.num++;
                   console.log(state.num += a); 
                   
                 }
             },
             getters:{
                 getAge:function(state){
                     return state.age;
                 }
             },
             actions:{
                 //
             }
        });
    
        Vue.component('hello',{
            template:"<p @click='changeNum'>姓名:{{name}} 年龄:{{age}} 次数:{{num}}</p>",
            computed: {
                name:function(){
                    return this.$store.state.name
                },
                age:function(){
                    return this.$store.getters.getAge
                },
                num:function(){
                    return this.$store.state.num
                }
            },
             mounted:function(){
                console.log(this)
            },
            methods: {
                changeNum: function(){
                    //在组件里提交
                    // this.num++;
                    this.$store.commit('change',10)
                }
            },
            data:function(){
                return {
                    // num:5
                }
            }
        })
        new Vue({
            el:"#app",
            data:{
                name:"dk"
            },
            store:myStore,
            mounted:function(){
                console.log(this)
            }
        }) 
    输出 姓名:jjk 年龄:18 次数:1
    点击p标签后输出 姓名:jjk 年龄:18 次数:11

     可以看出:更改state的数据并显示在组件中,有几个步骤:1. 在mutations选项里,注册函数 函数里面装逻辑代码。2.在组件里,this.$store.commit('change',payload)  注意:提交的函数名要一一对应  3.触发函数,state就会相应更改 4.在组件的计算属性里 this.$store.state 获取你想要得到的数据

          actions:既然mutations只能处理同步函数,我大js全靠‘异步回调’吃饭,怎么能没有异步,于是actions出现了...  

            actions和mutations的区别

              1.Actions 提交的是 mutations,而不是直接变更状态。也就是说,actions会通过mutations,让mutations帮他提交数据的变更。

              2.Action 可以包含任意异步操作。ajax、setTimeout、setInterval不在话下

         

  • 相关阅读:
    10分钟学会SpringBoot入门
    单链表常见的4道笔试题(Java版)
    Java面试、跳槽必刷200+真面试题,让你披荆斩棘走进大厂
    金三银四JAVA面试总结:Java+并发+Spring+MySQL+分布式+Redis+算法+JVM等
    最新整理的spring面试题从基础到高级,干货满满
    面试阿里百分百问的Jvm,别问有没有必要学,真的很有必要朋友
    面试官:你们前后端分离的接口规范是什么?
    “金九银十”已过,总结我的天猫、蚂蚁、头条面试经历(Java岗)
    350道面试题分享,拿下京东offer工资double
    2019大厂Java岗面试题全曝光,刷完这1020道,金三银四大厂等你
  • 原文地址:https://www.cnblogs.com/lqtbk/p/9516739.html
Copyright © 2011-2022 走看看