vue 组件通讯总结

news/2024/7/10 1:33:51 标签: vue, js, javascript

Props $emit 方式

父子传参最常用的方式就是使用props,$emit

1. 父传子

子组件从父组件接受参数通过props,并且可以规定类型,默认值等
总结

1.父组件中 v-bind:parentToOne="parentMsg" ,
一般parentToOne 与 parentMsg 名称相同即可
即:v-bind:parentMsg="parentMsg" 下面props接收也改为parentMsg,这里为了阐明关系
2.子组件中Props接收  parentToOne

父组件

js">	<template>
	  <div class="parent">
	    <SubOne :parentToOne="parentMsg"/>
	  </div>
	</template>
	<script>
	import SubOne from './components/SubOne'
	export default {
	  name:"parent",
	  data(){
	    return {
	      parentMsg:'parent',
	    }
	  },
	  components:{SubOne},
	}
	</script>

子组件

js"><template>
    <h6>来自父组件的参数parentMsg:{{parentToOne}}</h6>
</template>
<script>
	export default {
	  name: "SubOne",
	  props: {
	  //规定类型,默认值
	    parentToOne: {
	      type: String,
	      default: "xxx",
	    },
	  }
	};
</script>

2. 父传子,且子组件修改props传递参数,通过sync 修饰符

总结

js">1.父组件中在给传递给子组件的参数添加sync修饰符
2.子组件中通过$emit('update: propName',value)

父组件

js"><h3>I am父组件</h3>
<SubTwo :parentMsg.sync="parentMsg"/>

子组件

js">	<template>
	   <h6>
	     修改父数据:<input type="text" @input="msgChange">
	   </h6>
	</template>
	<script>
	export default {
	  name: "SubTwo",
	  //接受参数
	  props: {
	    parentMsg: {
	      type: String,
	      default: "xxx",
	    },
	  },
	  methods:{
	    msgChange:function($event){
	    //在子组件方法里 通过$emit修改
	      this.$emit('update:parentMsg',$event.target.value)
	    }
	  },
	};
	</script>

3. 子传父

总结

1.父组件里 给子组件添加v-on 监听并绑定父组件方法接收参数
2.子组件里面 $emit通过subThree 触发reserveSubMsg

父组件

js"><template>
  <div class="parent">
    <h3>I am 父组件</h3>
    <h6>接收subThree的参数 subMsg:{{subMsg}}</h6>
    <SubThree v-on:subThree="reserveSubMsg"/>
  </div>
</template>
<script>
import SubThree from './components/SubThree'
export default {
  name:"parent",
  data(){
    return {
      subMsg:''
    }
  },
  components:{
   SubThree
  },
  methods:{
    reserveSubMsg(value){
      this.subMsg = value
    }
  }
}
</script>

子组件

js"><template>
  <div class="sub">
      传递父数据:<input type="text" v-model="value" @input="msgChange">
  </div>
</template>
<script>
export default {
  name: "SubThree",
  data() {
    return {
      value:''
    };
  },
  methods: {
    msgChange(){
      this.$emit('subThree',this.value)
    }
  },
};
</script>

provide inject 方式

父子之间传递参数也是有缺点的,比如子组件嵌套很深的情况下,逐层传递会很麻烦,
provide-inject方式可以很方便的解决这个问题
总结

这种方式比较像发布订阅
1.在顶层组件中,通过provide 发布变量(与data同级)
2.顶层组件的子组件中都可以通过inject 接收到变量,可以直接通过this访问

顶级组件

js">  provide() {
    return {
      topFloorMsg: "come from top",
    };
  },

子组件

js">//省略部分代码 完整代码见下
  <h6>来自top组件的参数 topFloorMsg: {{topFloorMsg}}</h6>
  inject:['topFloorMsg'],

bus 非组件之间传值

总结

js">思路:实例化一个vue实例,利用它身上的$emit $on 
1. 生成实例,来做为中间传达的工具
2. 在任意组件 $emit 发布变量
3. 在任意组件 $on 订阅监听变量

js">//bus.js
import Vue from 'vue'
export default new Vue()

BusA

  // 引入公共的bus,来做为中间传达的工具
  import Bus from '@/bus.js'
  //方法中触发
   methods: {
    usHandle(){
      Bus.$emit('busMsg','come from bus')
    }
  },

BusB

js"> <h6>来自Bud事件总线的 busMsg: {{ value }}</h6>
  mounted(){
    var vm = this;
    // 用$on事件来接收参数
    Bus.$on("busMsg", (data) => {
      console.log(data);
      vm.value = data;
    });
  },

完整代码

父组件

js"><template>
  <div class="parent">
    <div class="item">
      <h3>父子传参 props $emit</h3>
      <h6>父组件参数 parentMsg:{{ parentMsg }}</h6>
      <h6>接收subThree的参数 subMsg:{{ subMsg }}</h6>
      <SubOne :parentToOne="parentMsg" />
      <SubTwo :parentMsg.sync="parentMsg" />
      <SubThree v-on:subThree="reserveSubMsg" />
    </div>
    <div class="item">
      <h3>Provide - Inject方式</h3>
      <Son />
    </div>
    <div class="item">
      <h3>Bus方式</h3>
      <button @click="busHandle">传递参数</button>
      <BusA/>
    </div>
  </div>
</template>
<script>
import SubOne from "./components/SubOne";
import SubTwo from "./components/SubTwo";
import SubThree from "./components/SubThree";
import Son from "./components/Son";
  // 引入公共的bug,来做为中间传达的工具
  import Bus from '@/bus.js'
import BusA from "./components/BusA";
  
export default {
  name: "parent",
  data() {
    return {
      parentMsg: "parent",
      subMsg: "",
    };
  },
  provide() {
    return {
      topFloorMsg: "come from top",
    };
  },
  components: {
    SubOne,
    SubTwo,
    SubThree,
    Son,BusA
  },
  methods: {
    reserveSubMsg(value) {
      this.subMsg = value;
    },
    busHandle(){
      Bus.$emit('busMsg','come from bus')
    }
  },
};
</script>
<style lang="scss" scoped>
.parent {
  padding: 20px;
  background: #ffffff;
  border: 1px dashed pink;
  display: flex;
  justify-content: space-around;
  .item{
    width: 33%;
  }
}
</style>

SubOne

js"><template>
  <div class="sub">
    <h3>I am subOne 子组件 我专门接受父组件参数</h3>
    <h6>来自父组件的参数parentMsg:{{parentToOne}}</h6>
  </div>
</template>
<script>
export default {
  name: "SubOne",
  props: {
    parentToOne: {
      type: String,
      default: "xxx",
    },
  }
};
</script>
<style lang="scss" scoped>
.sub{
  border: 1px dashed #333332;
  background: lightblue;
}
</style>

SubTwo

js"><template>
  <div class="sub">
    <h3>I am subTwo 子组件 我接受并修改父组件参数</h3>
    <h6>来自父组件的参数parentMsg:{{parentMsg}}</h6>
    <h6>
      修改父数据:<input type="text" @input="msgChange">
    </h6>
  </div>
</template>
<script>
export default {
  name: "SubTwo",
  props: {
    parentMsg: {
      type: String,
      default: "xxx",
    },
  },
  methods:{
    msgChange:function($event){
      console.log($event.target.value);
      this.$emit('update:parentMsg',$event.target.value)
    }
  },
};
</script>
<style lang="scss" scoped>
.sub{
  margin-top: 20px;
  border: 1px dashed #333332;
  background: lightblue;
}
</style>

SubThree

js"><template>
  <div class="sub">
    <h3>
      I am subThree 子组件 我给父组件传递参数
    </h3>
      传递父数据:<input type="text" v-model="value" @input="msgChange">
  </div>
</template>
<script>
export default {
  name: "SubThree",
  data() {
    return {
      value:''
    };
  },
  components: {},
  methods: {
    msgChange($event){
      console.log($event.target.value);
      this.$emit('subThree',this.value)
    }
  },
};
</script>
<style lang="scss" scoped>
.sub{
  border: 1px dashed #333332;
  background: lightblue;
}
</style>

son

js"><template>
  <div class="sub">
    <h3>
      I am Son 子组件 
    </h3>
    <h6>来自top组件的参数 topFloorMsg: {{topFloorMsg}}</h6>
    <!-- <Grandson/> -->
  </div>
</template>
<script>
// import Grandson from './Grandson'
export default {
  name: "Son",
  inject:['topFloorMsg'],
  components:{
    // Grandson
  }
};
</script>
<style lang="scss" scoped>
.sub{
  border: 1px dashed #333332;
  background: lightblue;
}
</style>

BusA.vue

js"><template>
  <div class="sub">
    <h3>
      I am BusA 子组件
    </h3>
    <h6>来自Bud事件总线的 busMsg: {{ value }}</h6>
  </div>
</template>
<script>
import Bus from "@/bus.js";
export default {
  name: "BusA",
  data(){
    return{
      value:''
    }
  },
  mounted(){
    var vm = this;
    
    // 用$on事件来接收参数
    Bus.$on("busMsg", (data) => {
      console.log(data);
      vm.value = data;
    });
  },
};
</script>
<style lang="scss" scoped>
.sub {
  border: 1px dashed #333332;
  background: lightblue;
}
</style>


http://www.niftyadmin.cn/n/1852472.html

相关文章

某网络专业人士笔记(非常值得收藏)2

4、执行路由核心复制 core dump包含一份当前系统内存中信息的精确拷贝。捕捉包含在内存中信息的方法有&#xff1a; 1&#xff09; 配置路由器在崩溃时执行Core Dump&#xff0c;存储到TFTP、FTP、RCP服务器&#xff1a; 对TFTP协议&#xff0c;只需指定TFTP服务器IP&#xff0…

JavaWeb项目-超市订单管理系统【Day02】

密码修改 1、编写接口方法和mybatis的SQL映射文件 Mybatis配置多参数SQL语句 当我们的SQL语句中有多个参数的时候&#xff0c;需要设置每个参数名对应的接口参数&#xff0c;不然会报错&#xff1a; Parameter ‘id’ not found. Available parameters are [argl, argg, par…

vue vuex模块化

简单介绍 state //store.js //存放数据state: {index:index in root store}, //组件内获取方式computed: {// 可以结合computed 使用index(){return this.$store.state.index}}mutations mutations两个参数 第一个是默认参数state 第二个是传参payload,这里有一个异步同步的…

vue WebWorker的使用

webworker 我们知道javascript是单线程&#xff0c;当主线程遇到大量计算或者复杂的业务逻辑时&#xff0c;会对我们的页面造成不好的用户体验。 webworker 很好的解决了这个问题&#xff0c;我们可以在主线程开启一个worker线程执行任务而不干扰用户界面&#xff08;主线程&a…

SE100101系统概述

目录课程说明.................................................................................................................................. 1<?xml:namespace prefix o ns "urn:schemas-microsoft-com:office:office" />课程介绍.............…

CSS 常见的兼容性问题

1.浏览器私有前缀 五大浏览器内核 1.Gecko内核 -moz- 火狐浏览器 2.Webkit -webkit- 谷歌内核 safari 买去 3.Trident -ms- IE内核 4.Presto内核 -o- 只有opera 采用 5.blink内核 -webkit- 谷歌采用标准写法 -moz-animation: ; -o-animation: ; -webkit-animation: ;…

潘石屹能否拯救中国楼市?

潘石屹能否拯救中国楼市&#xff1f; 时寒冰在楼市成交低迷&#xff0c;大调整渐渐逼近之时&#xff0c;在一个月黑风高之夜&#xff0c;一个瘦弱的汉子壮烈地高呼&#xff1a;“我不涨价谁涨价&#xff01;” 北京震动了&#xff0c;神州大地震动了&#xff0c;整个世界…

CSS3新特性总结

CSS3选择器 border-radius圆角边框 // 两个参数 8个值 border-radius:10px 20px 30px 40px / 10px 20px 30px 40px //左上角开始 border-radius:10px 20px //对角线 // 两个参数 8个值 border-radius:5%; border-radius:50%; shadow 阴影 盒子阴影 //一参&#xff1a;横向…