Vue3父子通讯方式及Vue3插槽的使用方法是什么

发布时间:2023-01-28 15:17:38 作者:iii
来源:亿速云 阅读:103

这篇文章主要介绍了Vue3父子通讯方式及Vue3插槽的使用方法是什么的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Vue3父子通讯方式及Vue3插槽的使用方法是什么文章都会有所收获,下面我们一起来看看吧。

Vue3父传子(props)

父组件如下:

<template>
  <div class="about">
    <h2>This is an about page</h2>
    <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>

Vue3父子通讯方式及Vue3插槽的使用方法是什么

setup中的参数分别有:

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

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

Vue3子传父({emit})

父组件:

<template>
  <div class="about">
    <h2>This is an about page</h2>
    <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父子通讯方式及Vue3插槽的使用方法是什么

Vue3插槽

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

Vue3父子通讯方式及Vue3插槽的使用方法是什么

具名插槽的写法

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

关于“Vue3父子通讯方式及Vue3插槽的使用方法是什么”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“Vue3父子通讯方式及Vue3插槽的使用方法是什么”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. vue3.0新特性是什么
  2. 如何用40行代码把Vue3的响应式集成进React做状态管理

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

vue3

上一篇:go微服务PolarisMesh服务端启动流程是什么

下一篇:怎么使用Bazel构建Golang程序

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》