经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » Vue.js » 查看文章
Vue3父子通讯方式及Vue3插槽的使用方法详解
来源:jb51  时间:2023/1/28 8:43:05  对本文有异议

在Vue3中父子通讯方式

Vue3父传子(props)

父组件如下:

  1. <template>
  2. <div class="about">
  3. <h1>This is an about page</h1>
  4. <children :num="num" age="30"></children>
  5. </div>
  6. </template>
  7. <script>
  8. import children from "../components/children.vue";
  9. import { ref } from "vue";
  10. export default {
  11. setup() {
  12. let num = ref("《nanchen》");
  13. return {
  14. num,
  15. };
  16. },
  17. components: {
  18. children,
  19. },
  20. };
  21. </script>

子组件如下:

  1. <template>
  2. <div>我是子组件 我的父组件值为:{{ yy }}</div>
  3. </template>
  4. <script>
  5. import { ref } from "vue";
  6. export default {
  7. name: "Vue3appChildren",
  8. props: {
  9. num: {
  10. type: Number,
  11. },
  12. },
  13. setup(props) {
  14. let yy = ref(props.num);
  15. return {
  16. yy,
  17. };
  18. },
  19. mounted() {},
  20. methods: {},
  21. };
  22. </script>
  23. <style lang="scss" scoped>
  24. </style>

setup中的参数分别有:

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

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

Vue3子传父({emit})

父组件:

  1. <template>
  2. <div class="about">
  3. <h1>This is an about page</h1>
  4. <children :num="num" age="30" @test="showHello"></children>
  5. </div>
  6. </template>
  7. <script>
  8. import children from "../components/children.vue";
  9. import { ref } from "vue";
  10. export default {
  11. setup() {
  12. let num = ref("《nanchen》");
  13. function showHello(value) {
  14. console.log(value);
  15. }
  16. return {
  17. num,
  18. showHello,
  19. };
  20. },
  21. components: {
  22. children,
  23. },
  24. };
  25. </script>

子组件

  1. <template>
  2. <div @click="aboutClick">我是子组件 我的父组件值为:{{ yy }}</div>
  3. </template>
  4. <script>
  5. import { ref } from "vue";
  6. export default {
  7. name: "Vue3appChildren",
  8. props: {
  9. num: {
  10. type: Number,
  11. },
  12. },
  13. setup(props, { emit }) {
  14. let yy = ref(props.num);
  15. function aboutClick() {
  16. emit("test", "你好你好"); // 子传父
  17. }
  18. return {
  19. yy,
  20. aboutClick,
  21. };
  22. },
  23. mounted() {},
  24. methods: {},
  25. };
  26. </script>
  27. <style lang="scss" scoped>
  28. </style>

点击div效果如下:

Vue3插槽

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

具名插槽的写法

  1. <slot name="aabb"></slot>
  1. <HelloWorld>
  2. <template v-slot:aabb>
  3. <span>NanChen,你好</span>
  4. </template>
  5. <!-- <template #aabb>
  6. <span>NanChen,你好</span>
  7. </template> -->
  8. </HelloWorld>

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

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号