Vue3父子通讯方式及Vue3插槽的使用方法详解

 更新时间:2023年01月26日 12:29:10   作者:Nanchen_42  
这篇文章主要介绍了Vue3父子通讯方式及Vue3插槽的使用方法详解,需要的朋友可以参考下

在Vue3中父子通讯方式

Vue3父传子(props)

父组件如下:

<template>
  <div class="about">
    <h1>This is an about page</h1>
    <children :num="num" age="30"></children>
  </div>
</template>
<script>
import children from "../components/children.vue";
import { ref } from "vue";
export default {
  setup() {
    let num = ref("《nanchen》");
    return {
      num,
    };
  },
  components: {
    children,
  },
};
</script>

子组件如下:

<template>
  <div>我是子组件 我的父组件值为:{{ yy }}</div>
</template>
 
<script>
import { ref } from "vue";
export default {
  name: "Vue3appChildren",
  props: {
    num: {
      type: Number,
    },
  },
  setup(props) {
    let yy = ref(props.num);
    return {
      yy,
    };
  },
 
  mounted() {},
 
  methods: {},
};
</script>
 
<style lang="scss" scoped>
</style>

setup中的参数分别有:

props:值为对象,包含:组件外部传递过来,且组件内部声明接收了的属性。
context:上下文对象
attrs: 值为对象,包含:组件外部传递过来,但没有在props配置中声明的属性, 相当于 this.$attrs。
slots: 收到的插槽内容, 相当于 this.$slots。
emit: 分发自定义事件的函数, 相当于 this.$emit

props中可以接收父组件传递给子组件的参数 

Vue3子传父({emit})

父组件:

<template>
  <div class="about">
    <h1>This is an about page</h1>
    <children :num="num" age="30" @test="showHello"></children>
  </div>
</template>
<script>
import children from "../components/children.vue";
import { ref } from "vue";
export default {
  setup() {
    let num = ref("《nanchen》");
    function showHello(value) {
      console.log(value);
    }
    return {
      num,
      showHello,
    };
  },
  components: {
    children,
  },
};
</script>

子组件

<template>
  <div @click="aboutClick">我是子组件 我的父组件值为:{{ yy }}</div>
</template>
 
<script>
import { ref } from "vue";
export default {
  name: "Vue3appChildren",
  props: {
    num: {
      type: Number,
    },
  },
  setup(props, { emit }) {
    let yy = ref(props.num);
    function aboutClick() {
      emit("test", "你好你好"); // 子传父
    }
    return {
      yy,
      aboutClick,
    };
  },
 
  mounted() {},
 
  methods: {},
};
</script>
 
<style lang="scss" scoped>
</style>

点击div效果如下:

Vue3插槽

    <children :num="num" age="30" @test="showHello">
      <h1>南辰,Hello</h1>
    </children>
<template>
  <div @click="aboutClick">我是子组件 我的父组件值为:{{ yy }}</div>
  <slot></slot>
</template>

具名插槽的写法

<slot name="aabb"></slot>
	<HelloWorld>
		<template v-slot:aabb>
			<span>NanChen,你好</span>
		</template>
		 <!-- <template #aabb>
			<span>NanChen,你好</span>
		</template> -->
	</HelloWorld>

到此这篇关于Vue3父子通讯方式及Vue3插槽的使用方法详解的文章就介绍到这了,更多相关Vue3父子通讯方式及Vue3插槽的使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • VUE2中的MVVM模式详解

    VUE2中的MVVM模式详解

    这篇文章主要为大家介绍了VUE2中的MVVM模式详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-05-05
  • VUE解决微信签名及SPA微信invalid signature问题(完美处理)

    VUE解决微信签名及SPA微信invalid signature问题(完美处理)

    这篇文章主要介绍了VUE解决微信签名及SPA微信invalid signature问题(完美处理),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-03-03
  • VUE2 前端实现 静态二级省市联动选择select的示例

    VUE2 前端实现 静态二级省市联动选择select的示例

    下面小编就为大家分享一篇VUE2 前端实现 静态二级省市联动选择select的示例。具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-02-02
  • vue resource post请求时遇到的坑

    vue resource post请求时遇到的坑

    这篇文章主要介绍了vue resource post请求时遇到的坑,需要的朋友可以参考下
    2017-10-10
  • vue 实现上传按钮的样式的两种方法

    vue 实现上传按钮的样式的两种方法

    这篇文章主要介绍了vue 定制上传按钮的样式的两种方法,本文结合示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-12-12
  • 详解vue的Des加密解密

    详解vue的Des加密解密

    这篇文章主要为大家介绍了vue的Des加密解密使用实例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09
  • 详解关于element el-button使用$attrs的一个注意要点

    详解关于element el-button使用$attrs的一个注意要点

    这篇文章主要介绍了详解关于element el-button使用$attrs的一个注意要点,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-11-11
  • vue2.x中$attrs的使用方法教程

    vue2.x中$attrs的使用方法教程

    正常情况下Vue推荐用props向子组件参数,但是在特定场景下,使用$attrs会更方便,下面这篇文章主要给大家介绍了关于vue2.x中$attrs使用的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-11-11
  • Vue列表循环从指定下标开始的多种解决方案

    Vue列表循环从指定下标开始的多种解决方案

    这篇文章主要介绍了Vue列表循环从指定下标开始的多种方案,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-04-04
  • vue中配置后端接口服务信息详解

    vue中配置后端接口服务信息详解

    这篇文章主要介绍了vue中配置后端接口服务信息详解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03

最新评论