</Children> // 在子组件上定义 props: [...">
最新公告
  • 欢迎您光临起源地模板网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!立即加入钻石VIP
  • 九种简洁的Vue数据通讯

    正文概述 掘金(哈哈哈同学)   2020-11-30   487

    props&@

    1. props

      • 数组形式

        // 父组件
        <Children v-bind:msg="msg"></Children>
        // 在子组件上定义
        props: ["msg"] //字符串对应的是父组件绑定的v-bind值.
        
      • 对象形式

        // 父组件
        <Children v-bind:msg="msg"></Children>
        // 在子组件上定义 -- 对象可以设置其他条件
        props: {
            msg: {
                type: String, //判断传入的类型
                default: "默认数据", //默认值
                require: true, // 是否必须传入项
            },
        },
        
    2. @

      // 在父组件把定义好的fn传下去,供子组件调用.
      <Children @fn="fn"></Children>
         
      //子组件通过$emit调用fn.
      this.$emit("fn", 1, 2, 3);
      

    ref

    //父组件
    <Children ref="subInst" /> 
    
    this.$refs.subInst // 父组件可以通过这个方法直接拿到子组件的data和methods.
    

    provide&inject

    //父组件上添加这个方法
    provide() {
        return {
    		msg: this.msg, // 这里需要注意传基本的值,没有响应式.需要传一个响应式的对象.
        };
    },
    
    // 子(孙)组件
    inject: ["msg"],
    

    attrs&listeners

    • $attrs

      //父组件
      <Children :msg="msg"></Children> 
        
      //子组件
      //传下来的数据保存在$attrs这个属性中
      <template>
        <div class="hello">
          我是attrs子组件 --- {{ $attrs.msg }} //$attrs上的值是只读的,并不会响应式的。强制$set设置vue会抛出警告.这是一个只读属性.
        </div>
      </template> 
      
    • $listeners

      //父组件
      <Children :fn="fn"></Children> 
        
      //子组件
      //传下来的方法保存在$listeners这个属性中,可直接调用.
      <template>
        <div class="hello">
          我是attrs子组件 --- {{ $attrs.msg }}
        </div>
      </template> 
      

    sync&model

    1. sync

      //父组件
      <Children :msg.sync="msg"></Children> // 在父组件注册的子组件挂载修饰符.sync
         
      // 子组件
      this.$emit("update:msg", "789"); //子组件上直接调用这个方法,传值改值.
      
    2. model

      //父组件
      <Children v-model="msg"></Children> // 在父组件注册的子组件v-model
         
      //子组件上使用需要props接收value和调用事件event的this.$emit("input", value);
      //这里默认使用input的v-model
      <input type="text" v-model="value" /> //子组件直接修改前提的接收props的value.
      

      在子组件可以通过model修改默认的value和event.例

      model: {
          prop: "check",
          event: "change",
      },
      

    eventBus

    // 在Vue的原型上绑定
    Vue.prototype.$eventBus = new Vue();
    
    //任意组件创建订阅callback这个方法,
    this.$eventBus.$on("callback", (data) => {
    console.log("接收到data----", data);
    });
    
    //任意组件上传入数据通讯,(发布)
    this.$eventBus.$emit("callback", "传入数据");
    

    parent&children

    • parent

      Vue.prototype.$dispatch = function (eventName, params) {
        var parent = this.$parent;
        while (parent) {
          parent.$emit(eventName, params);
          parent = parent.$parent;
        }
      } //在原型上定义个方法,内部就是一直往上级执行$emit这个方法.
      
      // 在需要使用组件数据的父组件中,订阅.
      this.$on("Up", function (params) {
      	this.msg = params;
      });
        
      // 调用方法的子组件或孙组件.
      this.$dispatch("Up", "我是通过parent方式改变的数据");
      
    • children

      // 通知子组件的监听事件
      Vue.prototype.$notice = function (eventName, params) {
        var children = this.$children;
        function deep(children) {
          children.forEach(item => {
            item.$emit(eventName, params);
            if (item.children) {
              deep(children);
            }
          });
        }
        deep(children);
      } //在原型上定义个方法,内部就是递归调用下级的$emit这个方法.
      
      // 在需要使用组件数据的子组件中,订阅.
      this.$on("down", function (params) {
      	this.msg = params;
      });
        
      // 调用方法的组件
      this.$notice("down", "我是通过children方式改变的数据");
      

    slot

    // 父组件
    <children>
      <template v-slot:header="scope">
        <h1>{{scope.header}}</h1> //这里展示的就是123.
      </template>
    </children>
    
    //子组件
    <slot name="header" :header="123"></slot>
    

    vuex

    这里就略过一下api吧.毕竟这个东西用vue的基本使用到这个东西.大家都很熟.

    • state

    • mutations

      const store = new Vuex.Store({
        state: {
          count: 1
        },
        mutations: {
          increment (state,a) { //后面是传进来的参数.
            // 变更状态
            state.count++
          }
        }
      })
        
      // 触发
      store.commit("increment",2)
      
    • actions

      • Action 提交的是 mutation,而不是直接变更状态。

      • Action 可以包含任意异步操作。

      const store = new Vuex.Store({
        state: {
          count: 0
        },
        mutations: {
          increment (state) {
            state.count++
          }
        },
        actions: {
          increment (context) {
            context.commit('increment')
          }
        }
      })
        
      //触发
      store.dispatch("increment"); // 这里需要异步操作返回promise.
      
    • getters

      const store = new Vuex.Store({
        state: {
          todos: [
            { id: 1, text: '...', done: true },
            { id: 2, text: '...', done: false }
          ]
        },
        getters: {
          doneTodos: state => {
            return state.todos.filter(todo => todo.done)
          }
        }
      })
        
      //访问
      store.getters.doneTodos
      
    • modules(略)

    • 辅助函数

      • mapState
      • mapActions
      • mapMutations
    • mapGetters

    总结


    起源地下载网 » 九种简洁的Vue数据通讯

    常见问题FAQ

    免费下载或者VIP会员专享资源能否直接商用?
    本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
    提示下载完但解压或打开不了?
    最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。若排除这种情况,可在对应资源底部留言,或 联络我们.。
    找不到素材资源介绍文章里的示例图片?
    对于PPT,KEY,Mockups,APP,网页模版等类型的素材,文章内用于介绍的图片通常并不包含在对应可供下载素材包内。这些相关商业图片需另外购买,且本站不负责(也没有办法)找到出处。 同样地一些字体文件也是这种情况,但部分素材会在素材包内有一份字体下载链接清单。
    模板不会安装或需要功能定制以及二次开发?
    请QQ联系我们

    发表评论

    还没有评论,快来抢沙发吧!

    如需帝国cms功能定制以及二次开发请联系我们

    联系作者

    请选择支付方式

    ×
    迅虎支付宝
    迅虎微信
    支付宝当面付
    余额支付
    ×
    微信扫码支付 0 元