vue3 - 组件通信

vue3 组件通信

  • 和vue2的通信方式基本一致,只是存在写法上的差异

props/emit

  • setup函数接收两个参数,props和context(上下文,其中有一个 emit)
  • 用法展示
1 // 父组件 2 <template> 3 <i-button :type='type' @onClick="click"> 按钮 </i-button> 4 </template> 5 <script> import { ref } from "vue" 6 import IButton from './IButton' 7 export default{ 8 components:{ 9 IButton 10 }, 11 setup(){ 12 const type = ref('primary'); 13 const click = val => { 14 // 父组件接收子组件的值 15 console.log(val) // 1 16 } 17 return{ 18 type, 19 click 20 } 21 } 22 } </script> 23 24 // 子组件 25 <template> 26 <button :type='type' @click="handleClick"> 27 <slot></slot> 28 </button> 29 </template> 30 <script> export default{ 31 props:{ 32 type:{ 33 type:String. 34 default:'default' 35 } 36 }, 37 setup(props,{ emit }){ 38 const handleClick = val => { 39 // 子组件向父组件传参 40 emit('onClick',1); 41 // 父组件监听的事件名要与这里注册的事件名保持一致 42 } 43 return{ 44 handleClick 45 } 46 } 47 } </script>

ref

  • vue2.x是通过 this.$children 等方法来获取的,但是 vue3中的setup函数中不能访问this,所以要使用其他的方法来获取实例对象
  • 用法展示
1 // 父组件 2 <template> 3 <i-button ref="btnRef" :type='type' @click="click"> 4 按钮 5 </i-button> 6 </template> 7 <script> import { ref } from "vue" 8 import IButton from './IButton' 9 export default{ 10 components:{ 11 IButton 12 }, 13 setup(){ 14 const type = ref('primary'); 15 const btnRef = ref(null); 16 const click = () => { 17 // 父组件调用子组件的方法来获取数据 18 const val = btnRef.value.sendParent(); 19 console.log(val); // 1 20 } 21 return{ 22 type, 23 btnRef, 24 click 25 } 26 } 27 } </script> 28 29 // 子组件 只写逻辑的部分 30 < script> 31 export default{ 32 setup(){ 33 const sendParent =()=>{ 34 return '1' 35 } 36 return { 37 sendParent // 一定要return出去 38 } 39 } 40 } 41 </script>

provide/inject

  • 个人觉得相对于2.x我也觉得使用更方便
  • 这种通信方法比较适合多层嵌套和平级组件使用
  • 缺点:不好追踪修改,导致代码混乱
  • 用法展示
1 // 父组件 只写逻辑的部分 2 < script> 3 import { ref, provide } from "vue"; 4 export default{ 5 setup(){ 6 const num = ref(0); 7 // 第一个参数是key值, 8 // 第二个参数是value 9 provide('num',num); 10 } 11 } 12 </script> 13 14 // 子组件 只写逻辑的部分 15 < script> 16 import { inject } from "vue"; 17 export default{ 18 setup(){ 19 const num = inject('num'); 20 console.log(num) // 0 21 } 22 } 23 </script>

vuex

  • vuex方式适合多组件数据统一,而且方便追溯
  • 用法展示(vuex的配置项不会的请参考:vuex4学习笔记)
1 // 父组件 只写逻辑的部分 2 < script> 3 import { useStore } from "vuex"; 4 export default{ 5 setup(){ 6 const store = useStore(); 7 8 const changeChildrenNum = () => { 9 store.dispatch("changeNum",1) 10 } 11 return { 12 changeChildrenNum 13 } 14 } 15 } 16 </script> 17 18 // 子组件 只写逻辑的部分 19 < script> 20 import { useStore,watch } from "vuex"; 21 export default{ 22 setup(){ 23 const store = useStore(); 24 watch( 25 () => store.getters.num, 26 (newVal,oldVal) => { 27 // 父组件点击触发 28 console.log(newVal); // 1 29 } 30 ) 31 32 return { 33 changeChildrenNum 34 } 35 } 36 } 37 </script>
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )