Vue3——03父子通讯方式,插槽的使用

简介: 父子通讯方式,插槽的使用

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>

a68d0e73f54e41ba9e948c430dde327c.png

setup中的参数分别有:

props:值为对象,包含:组件外部传递过来,且组件内部声明接收了的属性。

context:上下文对象

attrs: 值为对象,包含:组件外部传递过来,但没有在props配置中声明的属性, 相当于 this.$attrs。

slots: 收到的插槽内容, 相当于 this.$slots。

emit: 分发自定义事件的函数, 相当于 this.$emit

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

子传父({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效果如下:

761d714e53db40478004cf03cf1df9c8.png

插槽

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

c9ed27123a7046ec959238359a246d07.png

具名插槽的写法

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


相关文章
|
2天前
|
前端开发 JavaScript API
Vue3 五天速成(下)
Vue3 五天速成(下)
26 1
|
2天前
|
JavaScript 前端开发 网络架构
Vue3 五天速成(中)
Vue3 五天速成(中)
11 1
|
2天前
|
Web App开发 缓存 JavaScript
Vue3 五天速成(上)
Vue3 五天速成(上)
12 2
|
2天前
vue3版本的爱心源码
vue3版本的爱心源码
4 0
|
2天前
|
XML JavaScript 前端开发
Vue3 项目中怎么使用 jsx——易懂
Vue3 项目中怎么使用 jsx——易懂
5 0
|
2天前
|
JavaScript
vue3 实现电子签名
vue3 实现电子签名
6 1
|
2天前
|
JavaScript
vue3表格编辑(数据回显)和删除功能实现
vue3表格编辑(数据回显)和删除功能实现
7 1
|
1天前
|
JavaScript
Vue实战-组件通信
Vue实战-组件通信
4 0
|
1天前
|
JavaScript
Vue实战-将通用组件注册为全局组件
Vue实战-将通用组件注册为全局组件
5 0
|
1天前
|
JavaScript 前端开发
vue的论坛管理模块-文章评论02
vue的论坛管理模块-文章评论02