Vue 3 父子组件互调方法 - setup 语法糖写法

爱被打了一巴掌 2023-09-29 09:54 132阅读 0赞

Vue 3 父子组件互调方法 - setup 语法糖写法

文章目录

  • Vue 3 父子组件互调方法 - setup 语法糖写法
  • 一、父组件调用子组件方法
    • 1、子组件
    • 2、父组件
    • 3、测试结果
    • 4、关于 defineExpose 的官方文档
  • 二、子组件调用父组件方法
    • 1、子组件
    • 2、父组件
    • 3、测试结果
    • 4、关于 defineEmits 的官方文档
    • 5、仅限类型的 emit 声明

下面演示均为使用 setup 语法糖的情况!

一、父组件调用子组件方法

下面演示为使用 setup 语法糖的情况,值得注意的是子组件需要使用 defineExpose 对外暴露方法,父组件才可以调用!

1、子组件

  1. <template>
  2. </template>
  3. <script setup lang="ts">
  4. // 第一步:定义子组件里面的方法
  5. const doSth = (str: string) => {
  6. console.log("子组件的 doSth 方法执行了!" + str);
  7. }
  8. // 第二步:暴露方法
  9. defineExpose({ doSth })
  10. </script>
  11. <style scoped>
  12. </style>

2、父组件

  1. <template>
  2. <button @click="getChild">触发子组件方法</button>
  3. <!-- 第一步:定义 ref -->
  4. <HelloWorld ref="childRef" />
  5. </template>
  6. <script setup lang="ts">
  7. // 一、导入
  8. import { ref } from 'vue';
  9. import HelloWorld from './components/HelloWorld.vue';
  10. // 二、数据
  11. // 第二步:定义与 ref 同名变量
  12. const childRef = ref<any>();
  13. // 三、函数
  14. const getChild = () => {
  15. // 第三步: 调用子组件的方法或者变量,通过value
  16. childRef.value.doSth("随便传值!");
  17. }
  18. </script>
  19. <style>
  20. </style>

3、测试结果

请添加图片描述

4、关于 defineExpose 的官方文档

网址:https://v3.cn.vuejs.org/api/sfc-script-setup.html\#defineexpose

defineExpose

使用 <script setup> 的组件是默认关闭的,也即通过模板 ref 或者 $parent 链获取到的组件的公开实例,不会暴露任何在 <script setup> 中声明的绑定。

为了在 <script setup> 组件中明确要暴露出去的属性,使用 defineExpose 编译器宏:

  1. <script setup>
  2. import {
  3. ref } from 'vue'
  4. const a = 1
  5. const b = ref(2)
  6. defineExpose({
  7. a,
  8. b
  9. })
  10. </script>

当父组件通过模板 ref 的方式获取到当前组件的实例,获取到的实例会像这样 { a: number, b: number } (ref 会和在普通实例中一样被自动解包)。

二、子组件调用父组件方法

1、子组件

  1. <template>
  2. </template>
  3. <script setup lang="ts">
  4. import { onMounted } from "@vue/runtime-core";
  5. const emit = defineEmits([ "doSth" ]);
  6. const doSth = () => {
  7. emit('doSth');
  8. }
  9. onMounted(() => {
  10. doSth();
  11. });
  12. </script>
  13. <style scoped>
  14. </style>

2、父组件

  1. <template>
  2. <!-- 第一步:使用 @do-sth 或 @doSth 接受方法 -->
  3. <HelloWorld @doSth="sayHello" />
  4. </template>
  5. <script setup lang="ts">
  6. // 一、导入
  7. import HelloWorld from './components/HelloWorld.vue';
  8. // 二、函数
  9. // 第二步: 自定义方法
  10. const sayHello = () => {
  11. console.log("hello world!");
  12. }
  13. </script>
  14. <style>
  15. </style>

3、测试结果

请添加图片描述

4、关于 defineEmits 的官方文档

网址:https://v3.cn.vuejs.org/api/sfc-script-setup.html\#defineprops-和-defineemits

definePropsdefineEmits

<script setup> 中必须使用 definePropsdefineEmits API 来声明 propsemits ,它们具备完整的类型推断并且在 <script setup> 中是直接可用的:

  1. <script setup>
  2. const props = defineProps({
  3. foo: String
  4. })
  5. const emit = defineEmits(['change', 'delete'])
  6. // setup code
  7. </script>
  • definePropsdefineEmits 都是只在 <script setup> 中才能使用的编译器宏。他们不需要导入且会随着 <script setup> 处理过程一同被编译掉。
  • defineProps 接收与 props 选项相同的值,defineEmits 也接收 emits 选项相同的值。
  • definePropsdefineEmits 在选项传入后,会提供恰当的类型推断。
  • 传入到 definePropsdefineEmits 的选项会从 setup 中提升到模块的范围。因此,传入的选项不能引用在 setup 范围中声明的局部变量。这样做会引起编译错误。但是,它可以引用导入的绑定,因为它们也在模块范围内。

如果使用了 Typescript,使用纯类型声明来声明 prop 和 emits 也是可以的。

5、仅限类型的 emit 声明

  1. // 类型声明
  2. const emit = defineEmits<{
  3. (e: 'change', id: number): void
  4. (e: 'update', value: string): void
  5. }>()
  6. /**
  7. * 注意:
  8. * 1、只能是要么使用运行时声明,要么使用类型声明。同时使用两种声明方式会导致编译报错;
  9. * 2、使用类型声明的时候,静态分析会自动生成等效的运行时声明,以消除双重声明的需要并仍然确保正确的运行时行为。
  10. * ——来自官方文档
  11. */

发表评论

表情:
评论列表 (有 0 条评论,132人围观)

还没有评论,来说两句吧...

相关阅读

    相关 setup语法

    > 提示: vue3.2 版本开始才能使用语法糖! 1、如何使用setup语法糖 > 只需在 script 标签上写上 setup 代码如下(示例): <t