前端vue3.2组件通信详解(defineExpose、defineEmits、defineProps)使用方式

0oD三一o0

温馨提示:这篇文章已超过613天没有更新,请注意相关的内容是否还可用!

前言

在做vue项目中,总会遇到组件引入,在嵌套组件中我们的父级组件中引入子级组件中的值,或者在子组件中我们使用父组件中的值。当我们遇到这样的场景我们应该怎么做,在vue2.0中,我们使用props和emit进行父子之间的通信,兄弟之间用事件中央总线(event bus);在vue3.2的语法中我们则使用defineProps和defineEmits来声明props和emit,用其进行组件之间的传值,那么接下来,我们来看看。

defineProps

  1. 用于组件通信中父级组件给子级组件传值,其用来声明props,其接收值为props选项相同的值;

  2. 默认支持常见的类型检查,在ts下,我们需要明确变量的类型,类型经常是我们的自定义类型;

  3. 只能在<script setup>中使用;

  4. 不需要被导入即可使用,它会在编译<script setup>语法块时一同编译掉;

  5. 必须在<script setup>的顶层使用,不可以在<script setup>的局部变量中引用;

  6. 不可以访问 <script setup> 中定义的其他变量,因为在编译时整个表达式都会被移到外部的函数中。

复制/ 父级组件使用自定义属性向下传递值
<div class="home">
  <HelloWorld :msg="msg"/>
</div>
<script setup>
import HelloWorld from '@/components/HelloWorld'
/**
* 父级组件传递一个自定义属性
* 和props传递方式一样
* @type {string}
*/
const msg = '张三';
</script>
// 子级组件使用defineProps接收值
<div class="hello">
  <h1>{{ props.msg }}</h1>
</div>
<script setup>
/**
* 无需导入直接使用
* 写在<script setup>里面
* defineProps传入的对象key值就是传递的属性,父级传入msg,那么子级接收msg,定义其类型
* @type {Readonly<ExtractPropTypes<{msg: StringConstructor}>>}
* 以下props就是defineProps返回的对象
*/
const props = defineProps({
  msg: String,
});
</script>
<script setup>
 
/**
* 如果写在局部
* 报错:Uncaught ReferenceError: defineProps is not defined*/
const btn = function (){
  const props = defineProps({
    msg: String,
  });
}
</script>


defineEmits

  1. 用于组件通信中子级组件向父级组件传值,其用来声明emit,其接收内容于emit选项一致;

  2. 只能在<script setup>中使用;

  3. 不需要被导入即可使用,它会在编译<script setup>语法块时一同编译掉;

  4. 必须在<script setup>的顶层使用,不可以在<script setup>的局部变量中引用。

复制// 子组件
<div class="hello">
  <button @click="btn">点击</button>
</div>
<script setup>
/**
 * 在子组件中使用defineEmits来声明emits
 * 其接收值与emit选项一致
 * 传入的选项在全局变量
 * emits函数是defineEmits返回值
 * defineEmits函数参数是个数组,数组内容是自定义函数名称
 * emits函数第一个参数是自定义事件名称,第二个参数是需要传递的内容
 * defineEmits如果放在局部。不在全局。则报错
 * defineEmits is not defined
 * @type {EmitFn<string[]>}
 */
const emits = defineEmits(['handle']);
const btn = () => {
  emits('handle', '张三')
}
</script>
// 父级组件中
<div class="home">
  <HelloWorld @handle="handleClick"/>
</div
<script setup>
import HelloWorld from '@/components/HelloWorld'
/**
 * 在父级组件中,使用子级的自定义事件,
 * 在html中去写@自定义事件名称=事件名称
 * 函数中data是个形参,为子级传递过来的数据
 * @param data
 */
const handleClick = function (data) {
  console.log(data)
}
</script>

defineExpose

  1. 组件暴露自己的属性以及方法,去供外部使用,常用于父子组件关系;

  2. 在vue3.2中 setup 挂载到 script (ref默认是关闭的) 是不能直接使用 ref 取到子组件的方法和变量,需要使用defineExpose。

复制// 不在子组件设置defineExpose
// 子级组件代码
<div class="hello">
  <h1>{{name}}</h1>
</div>
<script setup>
  const name = '张三'
</script>
// 父级组件代码
<div class="home">
  <HelloWorld ref="childrenDom"/>
  <button @click='handleClick'>点击</button>
</div>
<script setup>
/**
 * 父级通常使用ref来定义虚拟dom,用来操作子组件
 * 那么这个时候打印ref的value值,我们拿不到子组件的属性以及方法
 */
import HelloWorld from '@/components/HelloWorld'
import { ref } from "vue";
const childrenDom = ref(null)
const handleClick = function () {
  console.log(childrenDom.value)
}
</script>


复制// 子组件设置defineExpose
// 子组件代码
<div class="hello">
  <h1>{{name}}</h1>
</div>
<script setup>
/**
 * 子组件中设置defineExpose等同于将子组件中的方法以及属性全部做出暴漏
 * 只需要父级通过ref去获取就可以
 * @type {string}
 */
const name = '张三';
const btn = function () {
  console.log(111)
}
defineExpose({
  name,
  btn
})
</script>
// 父级组件代码
<div class="home">
  <HelloWorld ref="childrenDom"/>
  <button @click='handleClick'>点击</button>
</div>
<script setup>
/**
 * 父级通常使用ref来定义虚拟dom,用来操作子组件
 * 那么这个时候打印ref的value值,我们拿不到子组件的属性以及方法
 */
import HelloWorld from '@/components/HelloWorld'
import { ref } from "vue";
const childrenDom = ref(null)
const handleClick = function () {
  console.log(childrenDom.value)
}
</script>


以上就是前端vue3.2组件通信详解(defineExpose、defineEmits、defineProps)的使用方式,感谢阅读。

免责声明:本文来自码云笔记,不代表0oD三一o0的观点和立场,如有侵权请联系本平台处理。

发表评论

快捷回复: 表情:
AddoilApplauseBadlaughBombCoffeeFabulousFacepalmFecesFrownHeyhaInsidiousKeepFightingNoProbPigHeadShockedSinistersmileSlapSocialSweatTolaughWatermelonWittyWowYeahYellowdog
评论列表 (暂无评论,4523人围观)

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

取消
微信二维码
微信二维码
支付宝二维码