Vue常用的组件通信方式有哪些

本篇文章和大家了解一下Vue常用的组件通信方式有哪些。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

目前创新互联建站已为成百上千家的企业提供了网站建设、域名、虚拟空间、网站托管维护、企业网站设计、丰林网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

组建通信的基本模式:父子组件的关系可以总结为 prop 向下传递,事件向上传递。父组件通过 prop 给子组件下发数据,子组件通过事件给父组件发送消息

Vue常用的组件通信方式有哪些
组件通信的常用三种方式

1.props(父组件向子组件传值)

parent.vue

 <template>
  <p>
    <parent-child :childName="childName"></parent-child>
  </p>
</template>

<script>
  import child from "./child"
  export default {
    components: {
      "parent-child" : child
    },data(){
      return {
        childName : "我是child哦"
      }
    }
  }
</script>

child.vue

<template>
  <p id="child">
    child的名字叫:{{childName}}<br>
  </p>
</template>

<script>
  export default {
    props:{
      childName :{
        type:String,
        default: ""
      }
    }
  }
</script>

2.$emit(子组件向父组件传值–局部消息订阅)

parent.vue

<template>
  <p>
    <parent-child :childName="childName" @childNotify="childReceive"></parent-child>
  </p>
</template>

<script>
  import child from "./child"
  export default {
    components: {
      "parent-child" : child
    },data(){
      return {
        childName : "我是child哦"
      }
    },methods:{
      childReceive(params){
        this.$message(params)
      }
    }
  }
</script>

child.vue

<template>
  <p id="child">
    child的名字叫:{{childName}}<br>
  </p>
</template>

<script>
  export default {
    props:{
      childName :{
        type:String,
        default: ""
      }
    },methods:{
      childNotify(params){
        this.$emit("childNotify","child的名字叫"+this.childName);
      }
    }
  }
</script>

3.bus全局消息订阅

bus.js

const install = (Vue) => {
  const Bus = new Vue({
    methods: {
      on (event, ...args) {
        this.$on(event, ...args);
      },
      emit (event, callback) {
        this.$emit(event, callback);
      },
      off (event, callback) {
        this.$off(event, callback);
      }
    }
  })
  Vue.prototype.$bus = Bus;
}
export default install;

main.js

import Bus from "./assets/js/bus";
Vue.use(Bus);

child.vue

<template>
  <p id="child">
    child的名字叫:{{childName}}<br>
    <el-button type="primary" @click="brotherNotity">向child2打招呼</el-button>
  </p>
</template>

<script>
  export default {
    props:{
      childName :{
        type:String,
        default: ""
      }
    },methods:{
      childNotify(params){
        this.$emit("childNotify","child的名字叫"+this.childName);
      },
      brotherNotity(){
        this.$bus.$emit("child2","child2你好呀");
      }
    }
  }
</script>

child2.vue

<template>
  <p id="child2">
    child2的名字叫:{{child2Name}}
  </p>
</template>

<script>
  export default {
    props:{
      child2Name :{
        type:String,
        default: ""
      }
    },
    created(){
      this.$bus.$on("child2",function(params){
        this.$message(params)
      })
    },
    beforeDestroy() {
      this.$bus.$off("child2",function(){
        this.$message("child2-bus销毁")
      })
    }
  }
</script>

vue是什么

Vue是一套用于构建用户界面的渐进式JavaScript框架,Vue与其它大型框架的区别是,使用Vue可以自底向上逐层应用,其核心库只关注视图层,方便与第三方库和项目整合,且使用Vue可以采用单文件组件和Vue生态系统支持的库开发复杂的单页应用。

以上就是Vue常用的组件通信方式有哪些的简略介绍,当然详细使用上面的不同还得要大家自己使用过才领会。如果想了解更多,欢迎关注创新互联行业资讯频道哦!

分享名称:Vue常用的组件通信方式有哪些
分享URL:https://www.cdcxhl.com/article40/pdspeo.html

成都网站建设公司_创新互联,为您提供做网站网站设计用户体验网站维护定制开发

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

h5响应式网站建设