首页 > 其他分享 >vue3 父子组件通信 setup

vue3 父子组件通信 setup

时间:2023-09-22 10:35:35浏览次数:31  
标签:vue const setup value vue3 组件 import ref

父传子

father

<template>
  <div>
    <h2>父传子Father</h2>

    <!-- <button class="bg-green-300 rounded p-1" >
      父按钮
    </button> -->
    <div class="w-[200px] h-[200px] bg-violet-200">
      <!-- :msg="fathermessage"父组件通过绑定msg传值给子组件 -->
      <fatherTransChildChild :msg="fathermessage" />
    </div>
  </div>
</template>
<script setup>
import fatherTransChildChild from "../components/FatherTransChild_Child.vue";
import { ref } from "vue";

const fathermessage = ref({ key: "Father", value: "父" });
</script>

child

<template>
  <div>
    <h2>父传子Child</h2>
    <div>拿到父组件的msg:{{ msg.key }}</div>
  </div>
</template>
<script setup>
// const prop = defineProps(["msg"]);
// prop.msg.key = "father";
// console.log(prop.msg.key);

//子组件接收父组件的值msg
defineProps({
  msg: Object,
});
</script>

子传父

father

<template>
  <div>
    <h2>子传父Father</h2>
    <div>拿到子组件的msg{{ sonMsg }}</div>
    <div class="w-[200px] h-[200px] bg-violet-200">
      <child-trans-father-child @get-value="getSonMsg" />
    </div>
  </div>
</template>
<script setup>
import ChildTransFatherChild from "../components/ChildTransFather_Child.vue";
import { ref } from "vue";

const sonMsg = ref("");

const getSonMsg = (value) => {
  sonMsg.value = value;
};
</script>

child

<template>
  <div>
    <h2>子传父Child</h2>
    <button @click="transValue" class="bg-blue-500">传值给父组件</button>
  </div>
</template>
<script setup>
import { ref } from "vue";
//子组件给父组件传值

// 定义所要传给父组件的值
const value = ref("c-message");

// 使用defineEmits注册一个自定义事件
const emit = defineEmits(["getValue"]);

const transValue = () => {
  emit("getValue", value.value);
};
//不想要触发直接 emit("getValue", value.value);即可
</script>

父调子

father

<template>
  <div>
    <h2>父调用子Father</h2>
    <button @click="getSonMethod" class="bg-blue-500">获取子组件的方法</button>
    <div class="w-[200px] h-[200px] bg-violet-200">
      <father-invoke-child-child ref="sonMethodRef" />
    </div>
  </div>
</template>
<script setup>
import FatherInvokeChildChild from "../components/FatherInvokeChild_Child.vue";

import { ref } from "vue";

const sonMethodRef = ref();

const getSonMethod = () => {
  sonMethodRef.value.toFatherMethod();
  console.log(sonMethodRef.value.toFatherValue);
};
</script>

child

<template>
  <div>
    <h2>父调用子Child</h2>
  </div>
</template>
<script setup>
import { ref } from "vue";
// 暴露给父组件的值
const toFatherValue = ref("我是要暴露给父组件的值");

// 暴露给父组件的方法
const toFatherMethod = () => {
  console.log("我是要暴露给父组件的方法");
};

// 暴露方法和属性给父组件
defineExpose({ toFatherMethod, toFatherValue });
</script>

子调父

father

<template>
  <div>
    <h2>子调用父Father</h2>

    <div class="w-[200px] h-[200px] bg-violet-200">
      <child-invoke-father-child @fatherMethod="fatherMethod" />
    </div>
  </div>
</template>
<script setup>
import ChildInvokeFatherChild from "../components/ChildInvokeFather_Child.vue";
const fatherMethod = () => {
  console.log("调用了父组件的方法");
};
</script>

child

<template>
  <div>
    <h2>子调用父Child</h2>
    <button class="bg-blue-500" @click="childMethod()">获取父组件的方法</button>
  </div>
</template>
<script setup>
const emit = defineEmits(["fatherMethod"]);
//fatherMethod 是想要调用父组件的一个方法
const childMethod = () => {
  emit("fatherMethod");
};
</script>

标签:vue,const,setup,value,vue3,组件,import,ref
From: https://www.cnblogs.com/sxliu414/p/17721720.html

相关文章

  • 界面组件DevExpress WinForms v23.1 - 富文本编辑器等功能升级
    DevExpressWinForms拥有180+组件和UI库,能为WindowsForms平台创建具有影响力的业务解决方案。DevExpressWinForms能完美构建流畅、美观且易于使用的应用程序,无论是Office风格的界面,还是分析处理大批量的业务数据,它都能轻松胜任!DevExpressWinForm 控件已正式发布v23.1版本,此版......
  • VUE3+vite+arco design 项目初始化
    意见反馈系统总结创建项目首先选择一个文件夹进入命令窗口使用vite创建项目npmcreatevite@latest初始化项目后进入项目安装依赖npminstall运行npmrundev使用arcodesign组件库安装npminstall--save-dev@arco-design/web-vue引入import{createApp}fr......
  • Vue3组件通信方式
    Vue3组件通信方式不管是vue2还是vue3,组件通信方式很重要,不管是项目还是面试都是经常用到的知识点。比如:vue2组件通信方式props:可以实现父子组件、子父组件、甚至兄弟组件通信自定义事件:可以实现子父组件通信全局事件总线$bus:可以实现任意组件通信pubsub:发布订阅模式......
  • COMException: 检索 COM 类工厂中 CLSID 为 {DB8CBF1C-D6D3-11D4-AA51-00A024EE30BD}
    没有注册类(异常来自HRESULT:0x80040154(REGDB_E_CLASSNOTREG)) "没有注册类(异常来自HRESULT:0x80040154(REGDB_E_CLASSNOTREG))"一般有两种情况,我最近做项目都遇到了》第一种:(生成平台的问题) 解决方法:在项目属性里设置“生成”=>“目标平台”为x86而不是默认的......
  • 每日一题:vue3自定义指令大全(呕心沥血所作,附可运行项目源码)
    1.VUE常用指令大全本项目所有指令均为全局注册,使用时直接在组件中使用即可。指令目录:src/directives页面目录:src/views具体可查看源码1.1权限指令封装一个权限指令,在模板中根据用户权限来控制元素的显示或隐藏。permission.jsimport{ref,watchEffect}from'vue';c......
  • BootstrapBlazor.Splitting 加载动画组件
    BootstrapBlazor.Splitting加载动画组件介绍本Blazor组件依赖于BootstrapBlazor组件库开发,底层由Splitting.js和gsap.js实现。使用该组件库之前需要先安装BootstrapBlazor.Splitting组件独立包。可以通过nuget命令行安装NuGet\Install-PackageBootstrapBlazor.Splitting-V......
  • Node组件——Express简介
    Node组件——Express简介隐逸王关注IP属地:陕西0.1082020.06.0113:36:03字数1,984阅读5,552概述Express是目前最流行的基于Node.js的Web开发框架,可以快速地搭建一个完整功能的网站。Express上手非常简单,首先新建一个项目目录,假定叫做hello-world。$mkdirhello-......
  • 【HarmonyOS】一文教你如何通过内存图片方式使用image组件加载网络图片资源
    【关键字】内存图片方式、image组件、网络图片资源、api6、服务卡片1、写在前面之前写过一篇元服务卡片的开发指导,有需求的可以参考以下文章:【HarmonyOS】低代码开发之FA卡片开发流程在2.6初始化卡片部分,我们实现了加载网络资源的图片,但是直接使用image组件加载网络资源似乎在新版......
  • 【HarmonyOS】一文教你如何通过内存图片方式使用image组件加载网络图片资源
    ​【关键字】内存图片方式、image组件、网络图片资源、api6、服务卡片 1、写在前面之前写过一篇元服务卡片的开发指导,有需求的可以参考以下文章:【HarmonyOS】低代码开发之FA卡片开发流程在2.6初始化卡片部分,我们实现了加载网络资源的图片,但是直接使用image组件加载网络资......
  • React学习之类组件的this指向问题
    免责声明我们幼儿园有适合自己看的注释拉满版文档,目标是我奶来都能看懂(不是)。1.前置知识类this指向call、bind、apply待展开...欸嘿,我怎么什么都想不己来了1.1es6类的简单回顾   classPerson{    //构造器    constructor(name,age){ ......