找回密码
 立即注册
首页 业界区 业界 vue3组件通信与props

vue3组件通信与props

边书仪 2025-6-6 14:56:43
title: vue3组件通信与props
date: 2024/5/31 下午9:00:57
updated: 2024/5/31 下午9:00:57
categories:

  • 前端开发
tags:

  • Vue3组件
  • Props详解
  • 生命周期
  • 数据通信
  • 模板语法
  • Composition API
  • 单向数据流
1.png

Vue 3 组件基础

在 Vue 3 中,组件是构建用户界面的基本单位,它们是可复用的 Vue 实例,具有自己的模板、数据、方法等。组件化开发使得代码更加模块化,易于管理和维护。以下是 Vue 3 组件的基础知识:
1. 组件的创建与注册

在 Vue 3 中,组件需要先定义后使用。定义组件的方式有两种:全局注册和局部注册。
全局注册

全局注册的组件可以在任何地方使用,通过 app.component 方法进行注册:
  1. import { createApp } from 'vue';
  2. import App from './App.vue';
  3. const app = createApp(App);
  4. app.component('my-component', {
  5. <template>
  6. <template>
  7.   <ChildComponent @child-event="handleChildEvent" />
  8. </template><ChildComponent :prop-name="dynamicValue" />
  9. </template>// 组件选项
  10. });
  11. app.mount('#app');
复制代码
局部注册

局部注册的组件只能在注册它的组件内部使用,通常在组件的 components 选项中进行注册:
  1. export default {
  2. <template>
  3. <template>
  4.   <ChildComponent @child-event="handleChildEvent" />
  5. </template><ChildComponent :prop-name="dynamicValue" />
  6. </template>components: {
  7. <template>
  8. <template>
  9.   <ChildComponent @child-event="handleChildEvent" />
  10. </template><ChildComponent :prop-name="dynamicValue" />
  11. </template><template>
  12. <template>
  13.   <ChildComponent @child-event="handleChildEvent" />
  14. </template><ChildComponent :prop-name="dynamicValue" />
  15. </template>'my-component': {
  16. <template>
  17. <template>
  18.   <ChildComponent @child-event="handleChildEvent" />
  19. </template><ChildComponent :prop-name="dynamicValue" />
  20. </template><template>
  21. <template>
  22.   <ChildComponent @child-event="handleChildEvent" />
  23. </template><ChildComponent :prop-name="dynamicValue" />
  24. </template><template>
  25. <template>
  26.   <ChildComponent @child-event="handleChildEvent" />
  27. </template><ChildComponent :prop-name="dynamicValue" />
  28. </template>// 组件选项
  29. <template>
  30. <template>
  31.   <ChildComponent @child-event="handleChildEvent" />
  32. </template><ChildComponent :prop-name="dynamicValue" />
  33. </template><template>
  34. <template>
  35.   <ChildComponent @child-event="handleChildEvent" />
  36. </template><ChildComponent :prop-name="dynamicValue" />
  37. </template>}
  38. <template>
  39. <template>
  40.   <ChildComponent @child-event="handleChildEvent" />
  41. </template><ChildComponent :prop-name="dynamicValue" />
  42. </template>}
  43. }
复制代码
2. 组件选项

组件选项包括模板、数据、方法、生命周期钩子等。
模板 (Template)

组件的模板定义了组件的结构,可以使用 HTML 和 Vue 的模板语法:
  1. <template>
  2. <template>
  3. <template>
  4.   <ChildComponent @child-event="handleChildEvent" />
  5. </template><ChildComponent :prop-name="dynamicValue" />
  6. </template>
  7. <template>
  8. <template>
  9.   <ChildComponent @child-event="handleChildEvent" />
  10. </template><ChildComponent :prop-name="dynamicValue" />
  11. </template><template>
  12. <template>
  13.   <ChildComponent @child-event="handleChildEvent" />
  14. </template><ChildComponent :prop-name="dynamicValue" />
  15. </template><h1>{{ title }}</h1>
  16. <template>
  17. <template>
  18.   <ChildComponent @child-event="handleChildEvent" />
  19. </template><ChildComponent :prop-name="dynamicValue" />
  20. </template><template>
  21. <template>
  22.   <ChildComponent @child-event="handleChildEvent" />
  23. </template><ChildComponent :prop-name="dynamicValue" />
  24. </template><p>{{ content }}</p>
  25. <template>
  26. <template>
  27.   <ChildComponent @child-event="handleChildEvent" />
  28. </template><ChildComponent :prop-name="dynamicValue" />
  29. </template>
  30. </template>
复制代码
数据 (Data)

组件的数据是响应式的,需要是一个函数,返回一个数据对象:
  1. export default {
  2. <template>
  3. <template>
  4.   <ChildComponent @child-event="handleChildEvent" />
  5. </template><ChildComponent :prop-name="dynamicValue" />
  6. </template>data() {
  7. <template>
  8. <template>
  9.   <ChildComponent @child-event="handleChildEvent" />
  10. </template><ChildComponent :prop-name="dynamicValue" />
  11. </template><template>
  12. <template>
  13.   <ChildComponent @child-event="handleChildEvent" />
  14. </template><ChildComponent :prop-name="dynamicValue" />
  15. </template>return {
  16. <template>
  17. <template>
  18.   <ChildComponent @child-event="handleChildEvent" />
  19. </template><ChildComponent :prop-name="dynamicValue" />
  20. </template><template>
  21. <template>
  22.   <ChildComponent @child-event="handleChildEvent" />
  23. </template><ChildComponent :prop-name="dynamicValue" />
  24. </template><template>
  25. <template>
  26.   <ChildComponent @child-event="handleChildEvent" />
  27. </template><ChildComponent :prop-name="dynamicValue" />
  28. </template>title: 'Hello Vue 3',
  29. <template>
  30. <template>
  31.   <ChildComponent @child-event="handleChildEvent" />
  32. </template><ChildComponent :prop-name="dynamicValue" />
  33. </template><template>
  34. <template>
  35.   <ChildComponent @child-event="handleChildEvent" />
  36. </template><ChildComponent :prop-name="dynamicValue" />
  37. </template><template>
  38. <template>
  39.   <ChildComponent @child-event="handleChildEvent" />
  40. </template><ChildComponent :prop-name="dynamicValue" />
  41. </template>content: 'Welcome to Vue 3 component basics.'
  42. <template>
  43. <template>
  44.   <ChildComponent @child-event="handleChildEvent" />
  45. </template><ChildComponent :prop-name="dynamicValue" />
  46. </template><template>
  47. <template>
  48.   <ChildComponent @child-event="handleChildEvent" />
  49. </template><ChildComponent :prop-name="dynamicValue" />
  50. </template>};
  51. <template>
  52. <template>
  53.   <ChildComponent @child-event="handleChildEvent" />
  54. </template><ChildComponent :prop-name="dynamicValue" />
  55. </template>}
  56. }
复制代码
方法 (Methods)

组件的方法定义在 methods 选项中,可以在模板中通过事件绑定来调用:
  1. export default {
  2. <template>
  3. <template>
  4.   <ChildComponent @child-event="handleChildEvent" />
  5. </template><ChildComponent :prop-name="dynamicValue" />
  6. </template>methods: {
  7. <template>
  8. <template>
  9.   <ChildComponent @child-event="handleChildEvent" />
  10. </template><ChildComponent :prop-name="dynamicValue" />
  11. </template><template>
  12. <template>
  13.   <ChildComponent @child-event="handleChildEvent" />
  14. </template><ChildComponent :prop-name="dynamicValue" />
  15. </template>greet() {
  16. <template>
  17. <template>
  18.   <ChildComponent @child-event="handleChildEvent" />
  19. </template><ChildComponent :prop-name="dynamicValue" />
  20. </template><template>
  21. <template>
  22.   <ChildComponent @child-event="handleChildEvent" />
  23. </template><ChildComponent :prop-name="dynamicValue" />
  24. </template><template>
  25. <template>
  26.   <ChildComponent @child-event="handleChildEvent" />
  27. </template><ChildComponent :prop-name="dynamicValue" />
  28. </template>alert('Hello from Vue 3 component!');
  29. <template>
  30. <template>
  31.   <ChildComponent @child-event="handleChildEvent" />
  32. </template><ChildComponent :prop-name="dynamicValue" />
  33. </template><template>
  34. <template>
  35.   <ChildComponent @child-event="handleChildEvent" />
  36. </template><ChildComponent :prop-name="dynamicValue" />
  37. </template>}
  38. <template>
  39. <template>
  40.   <ChildComponent @child-event="handleChildEvent" />
  41. </template><ChildComponent :prop-name="dynamicValue" />
  42. </template>}
  43. }
复制代码
生命周期钩子 (Lifecycle Hooks)

Vue 3 提供了多个生命周期钩子,允许你在组件的不同阶段执行代码。例如:
  1. export default {
  2. <template>
  3. <template>
  4.   <ChildComponent @child-event="handleChildEvent" />
  5. </template><ChildComponent :prop-name="dynamicValue" />
  6. </template>created() {
  7. <template>
  8. <template>
  9.   <ChildComponent @child-event="handleChildEvent" />
  10. </template><ChildComponent :prop-name="dynamicValue" />
  11. </template><template>
  12. <template>
  13.   <ChildComponent @child-event="handleChildEvent" />
  14. </template><ChildComponent :prop-name="dynamicValue" />
  15. </template>console.log('Component created');
  16. <template>
  17. <template>
  18.   <ChildComponent @child-event="handleChildEvent" />
  19. </template><ChildComponent :prop-name="dynamicValue" />
  20. </template>},
  21. <template>
  22. <template>
  23.   <ChildComponent @child-event="handleChildEvent" />
  24. </template><ChildComponent :prop-name="dynamicValue" />
  25. </template>mounted() {
  26. <template>
  27. <template>
  28.   <ChildComponent @child-event="handleChildEvent" />
  29. </template><ChildComponent :prop-name="dynamicValue" />
  30. </template><template>
  31. <template>
  32.   <ChildComponent @child-event="handleChildEvent" />
  33. </template><ChildComponent :prop-name="dynamicValue" />
  34. </template>console.log('Component mounted');
  35. <template>
  36. <template>
  37.   <ChildComponent @child-event="handleChildEvent" />
  38. </template><ChildComponent :prop-name="dynamicValue" />
  39. </template>}
  40. }
复制代码
3. 组件通信

组件之间的通信是 Vue 应用中的常见需求,Vue 3 提供了多种通信方式,包括 props、自定义事件、插槽等。
Props

Props 允许父组件向子组件传递数据:
  1. // 子组件
  2. export default {
  3. <template>
  4. <template>
  5.   <ChildComponent @child-event="handleChildEvent" />
  6. </template><ChildComponent :prop-name="dynamicValue" />
  7. </template>props: {
  8. <template>
  9. <template>
  10.   <ChildComponent @child-event="handleChildEvent" />
  11. </template><ChildComponent :prop-name="dynamicValue" />
  12. </template><template>
  13. <template>
  14.   <ChildComponent @child-event="handleChildEvent" />
  15. </template><ChildComponent :prop-name="dynamicValue" />
  16. </template>message: String
  17. <template>
  18. <template>
  19.   <ChildComponent @child-event="handleChildEvent" />
  20. </template><ChildComponent :prop-name="dynamicValue" />
  21. </template>}
  22. }
  23. // 父组件
  24. <child-component :message="hello"></child-component>
复制代码
自定义事件

子组件可以通过自定义事件向父组件传递数据:
  1. // 子组件
  2. this.$emit('my-event', data);
  3. // 父组件
  4. <child-component @my-event="handleEvent"></child-component>
复制代码
插槽 (Slots)

插槽允许父组件向子组件传递内容:
  1. <slot></slot>
  2. <child-component>
  3. <template>
  4. <template>
  5.   <ChildComponent @child-event="handleChildEvent" />
  6. </template><ChildComponent :prop-name="dynamicValue" />
  7. </template><p>This content is passed to the child component via slot.</p>
  8. </child-component>
复制代码
4. 组件的复用与组合

Vue 3 鼓励组件的复用和组合,通过 props、插槽等方式,可以构建出高度可复用的组件库。同时,Vue 3 还引入了 Composition API,使得组件的逻辑更加清晰和易于复用。
什么是 props?

在 Vue.js 框架中,props 是一个组件的属性,它可以接收来自父组件的数据,并将其传递到子组件中。props 使得子组件能够接收外部传入的信息,从而可以在不需要知道外部具体细节的情况下,实现与父组件的交互和数据传递。
Props 的作用


  • 传参:父组件可以通过 props 将数据传递给子组件。
  • 验证数据类型:在定义 props 时,可以指定期望的数据类型,这样 Vue 会在开发过程中进行类型检查,并在浏览器控制台中抛出警告,有助于提前发现潜在问题。
  • 设置默认值:如果父组件没有传递相应的 prop,可以设置默认值以确保子组件能够接收到一个合理的默认值。
Props 的使用

在 Vue 3 中,定义 props 的方式有几种,包括使用字符串数组、对象形式以及使用 TypeScript 的类型注解。
字符串数组形式
  1. export default {
  2. <template>
  3. <template>
  4.   <ChildComponent @child-event="handleChildEvent" />
  5. </template><ChildComponent :prop-name="dynamicValue" />
  6. </template>props: ['message']
  7. }
复制代码
这表示组件期望接收一个名为 message 的 prop,它是一个字符串类型。
对象形式
  1. export default {
  2. <template>
  3. <template>
  4.   <ChildComponent @child-event="handleChildEvent" />
  5. </template><ChildComponent :prop-name="dynamicValue" />
  6. </template>props: {
  7. <template>
  8. <template>
  9.   <ChildComponent @child-event="handleChildEvent" />
  10. </template><ChildComponent :prop-name="dynamicValue" />
  11. </template><template>
  12. <template>
  13.   <ChildComponent @child-event="handleChildEvent" />
  14. </template><ChildComponent :prop-name="dynamicValue" />
  15. </template>message: String,
  16. <template>
  17. <template>
  18.   <ChildComponent @child-event="handleChildEvent" />
  19. </template><ChildComponent :prop-name="dynamicValue" />
  20. </template><template>
  21. <template>
  22.   <ChildComponent @child-event="handleChildEvent" />
  23. </template><ChildComponent :prop-name="dynamicValue" />
  24. </template>title: {
  25. <template>
  26. <template>
  27.   <ChildComponent @child-event="handleChildEvent" />
  28. </template><ChildComponent :prop-name="dynamicValue" />
  29. </template><template>
  30. <template>
  31.   <ChildComponent @child-event="handleChildEvent" />
  32. </template><ChildComponent :prop-name="dynamicValue" />
  33. </template><template>
  34. <template>
  35.   <ChildComponent @child-event="handleChildEvent" />
  36. </template><ChildComponent :prop-name="dynamicValue" />
  37. </template>type: String,
  38. <template>
  39. <template>
  40.   <ChildComponent @child-event="handleChildEvent" />
  41. </template><ChildComponent :prop-name="dynamicValue" />
  42. </template><template>
  43. <template>
  44.   <ChildComponent @child-event="handleChildEvent" />
  45. </template><ChildComponent :prop-name="dynamicValue" />
  46. </template><template>
  47. <template>
  48.   <ChildComponent @child-event="handleChildEvent" />
  49. </template><ChildComponent :prop-name="dynamicValue" />
  50. </template>default: 'Default Title'
  51. <template>
  52. <template>
  53.   <ChildComponent @child-event="handleChildEvent" />
  54. </template><ChildComponent :prop-name="dynamicValue" />
  55. </template><template>
  56. <template>
  57.   <ChildComponent @child-event="handleChildEvent" />
  58. </template><ChildComponent :prop-name="dynamicValue" />
  59. </template>}
  60. <template>
  61. <template>
  62.   <ChildComponent @child-event="handleChildEvent" />
  63. </template><ChildComponent :prop-name="dynamicValue" />
  64. </template>}
  65. }
复制代码
这种方式下,message 被指定为字符串类型,而 title 被指定为字符串类型,并且有一个默认值。
TypeScript 类型注解

在使用 TypeScript 时,可以利用类型注解来定义 props:
  1. export default {
  2. <template>
  3. <template>
  4.   <ChildComponent @child-event="handleChildEvent" />
  5. </template><ChildComponent :prop-name="dynamicValue" />
  6. </template>props: {
  7. <template>
  8. <template>
  9.   <ChildComponent @child-event="handleChildEvent" />
  10. </template><ChildComponent :prop-name="dynamicValue" />
  11. </template><template>
  12. <template>
  13.   <ChildComponent @child-event="handleChildEvent" />
  14. </template><ChildComponent :prop-name="dynamicValue" />
  15. </template>message: string,
  16. <template>
  17. <template>
  18.   <ChildComponent @child-event="handleChildEvent" />
  19. </template><ChildComponent :prop-name="dynamicValue" />
  20. </template><template>
  21. <template>
  22.   <ChildComponent @child-event="handleChildEvent" />
  23. </template><ChildComponent :prop-name="dynamicValue" />
  24. </template>title: string
  25. <template>
  26. <template>
  27.   <ChildComponent @child-event="handleChildEvent" />
  28. </template><ChildComponent :prop-name="dynamicValue" />
  29. </template>}
  30. }
复制代码
Props 的传递

在父组件中,通过在模板中使用 v-bind 指令或者简写为 : 来传递 props:
  1. [/code]在这里,parentMessage 是父组件中的一个数据属性,它将被传递给名为 child-component 的子组件。
  2. [size=4]Props 的验证[/size]
  3. 在 Vue.js 中,props 验证是指对组件传入的 props 进行类型检查和默认值设置等验证。Vue 提供了一个可选的验证功能,可以在定义 props 时进行设置。以下是一些常见的 props 验证规则:
  4. [list=1]
  5. [*][b]类型验证[/b]:可以指定 props 的类型,如 String、Number、Boolean、Array、Object 等。
  6. [*][b]默认值[/b]:可以为 props 设置默认值,如果未传入 prop,则使用默认值。
  7. [*][b]必需性[/b]:可以指定 props 是否为必需,如果为必需,则必须在父组件中传入。
  8. [*][b]自定义验证[/b]:可以使用 validator 函数进行自定义验证。
  9. [*][b]单位转换[/b]:对于数值类型,可以指定单位,如 px、% 等,Vue 会自动进行单位转换。
  10. [/list]下面是一个使用 props 验证的例子:
  11. [code]export default {
  12. <template>
  13. <template>
  14.   <ChildComponent @child-event="handleChildEvent" />
  15. </template><ChildComponent :prop-name="dynamicValue" />
  16. </template>props: {
  17. <template>
  18. <template>
  19.   <ChildComponent @child-event="handleChildEvent" />
  20. </template><ChildComponent :prop-name="dynamicValue" />
  21. </template><template>
  22. <template>
  23.   <ChildComponent @child-event="handleChildEvent" />
  24. </template><ChildComponent :prop-name="dynamicValue" />
  25. </template>// 基本类型验证
  26. <template>
  27. <template>
  28.   <ChildComponent @child-event="handleChildEvent" />
  29. </template><ChildComponent :prop-name="dynamicValue" />
  30. </template><template>
  31. <template>
  32.   <ChildComponent @child-event="handleChildEvent" />
  33. </template><ChildComponent :prop-name="dynamicValue" />
  34. </template>title: {
  35. <template>
  36. <template>
  37.   <ChildComponent @child-event="handleChildEvent" />
  38. </template><ChildComponent :prop-name="dynamicValue" />
  39. </template><template>
  40. <template>
  41.   <ChildComponent @child-event="handleChildEvent" />
  42. </template><ChildComponent :prop-name="dynamicValue" />
  43. </template><template>
  44. <template>
  45.   <ChildComponent @child-event="handleChildEvent" />
  46. </template><ChildComponent :prop-name="dynamicValue" />
  47. </template>type: String,
  48. <template>
  49. <template>
  50.   <ChildComponent @child-event="handleChildEvent" />
  51. </template><ChildComponent :prop-name="dynamicValue" />
  52. </template><template>
  53. <template>
  54.   <ChildComponent @child-event="handleChildEvent" />
  55. </template><ChildComponent :prop-name="dynamicValue" />
  56. </template><template>
  57. <template>
  58.   <ChildComponent @child-event="handleChildEvent" />
  59. </template><ChildComponent :prop-name="dynamicValue" />
  60. </template>default: '默认标题'
  61. <template>
  62. <template>
  63.   <ChildComponent @child-event="handleChildEvent" />
  64. </template><ChildComponent :prop-name="dynamicValue" />
  65. </template><template>
  66. <template>
  67.   <ChildComponent @child-event="handleChildEvent" />
  68. </template><ChildComponent :prop-name="dynamicValue" />
  69. </template>},
  70. <template>
  71. <template>
  72.   <ChildComponent @child-event="handleChildEvent" />
  73. </template><ChildComponent :prop-name="dynamicValue" />
  74. </template><template>
  75. <template>
  76.   <ChildComponent @child-event="handleChildEvent" />
  77. </template><ChildComponent :prop-name="dynamicValue" />
  78. </template>// 数值类型验证,可以指定单位
  79. <template>
  80. <template>
  81.   <ChildComponent @child-event="handleChildEvent" />
  82. </template><ChildComponent :prop-name="dynamicValue" />
  83. </template><template>
  84. <template>
  85.   <ChildComponent @child-event="handleChildEvent" />
  86. </template><ChildComponent :prop-name="dynamicValue" />
  87. </template>width: {
  88. <template>
  89. <template>
  90.   <ChildComponent @child-event="handleChildEvent" />
  91. </template><ChildComponent :prop-name="dynamicValue" />
  92. </template><template>
  93. <template>
  94.   <ChildComponent @child-event="handleChildEvent" />
  95. </template><ChildComponent :prop-name="dynamicValue" />
  96. </template><template>
  97. <template>
  98.   <ChildComponent @child-event="handleChildEvent" />
  99. </template><ChildComponent :prop-name="dynamicValue" />
  100. </template>type: Number,
  101. <template>
  102. <template>
  103.   <ChildComponent @child-event="handleChildEvent" />
  104. </template><ChildComponent :prop-name="dynamicValue" />
  105. </template><template>
  106. <template>
  107.   <ChildComponent @child-event="handleChildEvent" />
  108. </template><ChildComponent :prop-name="dynamicValue" />
  109. </template><template>
  110. <template>
  111.   <ChildComponent @child-event="handleChildEvent" />
  112. </template><ChildComponent :prop-name="dynamicValue" />
  113. </template>default: 100,
  114. <template>
  115. <template>
  116.   <ChildComponent @child-event="handleChildEvent" />
  117. </template><ChildComponent :prop-name="dynamicValue" />
  118. </template><template>
  119. <template>
  120.   <ChildComponent @child-event="handleChildEvent" />
  121. </template><ChildComponent :prop-name="dynamicValue" />
  122. </template><template>
  123. <template>
  124.   <ChildComponent @child-event="handleChildEvent" />
  125. </template><ChildComponent :prop-name="dynamicValue" />
  126. </template>validator: (value) => {
  127. <template>
  128. <template>
  129.   <ChildComponent @child-event="handleChildEvent" />
  130. </template><ChildComponent :prop-name="dynamicValue" />
  131. </template><template>
  132. <template>
  133.   <ChildComponent @child-event="handleChildEvent" />
  134. </template><ChildComponent :prop-name="dynamicValue" />
  135. </template><template>
  136. <template>
  137.   <ChildComponent @child-event="handleChildEvent" />
  138. </template><ChildComponent :prop-name="dynamicValue" />
  139. </template><template>
  140. <template>
  141.   <ChildComponent @child-event="handleChildEvent" />
  142. </template><ChildComponent :prop-name="dynamicValue" />
  143. </template>return value >= 0; // 自定义验证,确保宽度非负
  144. <template>
  145. <template>
  146.   <ChildComponent @child-event="handleChildEvent" />
  147. </template><ChildComponent :prop-name="dynamicValue" />
  148. </template><template>
  149. <template>
  150.   <ChildComponent @child-event="handleChildEvent" />
  151. </template><ChildComponent :prop-name="dynamicValue" />
  152. </template><template>
  153. <template>
  154.   <ChildComponent @child-event="handleChildEvent" />
  155. </template><ChildComponent :prop-name="dynamicValue" />
  156. </template>}
  157. <template>
  158. <template>
  159.   <ChildComponent @child-event="handleChildEvent" />
  160. </template><ChildComponent :prop-name="dynamicValue" />
  161. </template><template>
  162. <template>
  163.   <ChildComponent @child-event="handleChildEvent" />
  164. </template><ChildComponent :prop-name="dynamicValue" />
  165. </template>},
  166. <template>
  167. <template>
  168.   <ChildComponent @child-event="handleChildEvent" />
  169. </template><ChildComponent :prop-name="dynamicValue" />
  170. </template><template>
  171. <template>
  172.   <ChildComponent @child-event="handleChildEvent" />
  173. </template><ChildComponent :prop-name="dynamicValue" />
  174. </template>// 数组类型验证
  175. <template>
  176. <template>
  177.   <ChildComponent @child-event="handleChildEvent" />
  178. </template><ChildComponent :prop-name="dynamicValue" />
  179. </template><template>
  180. <template>
  181.   <ChildComponent @child-event="handleChildEvent" />
  182. </template><ChildComponent :prop-name="dynamicValue" />
  183. </template>items: {
  184. <template>
  185. <template>
  186.   <ChildComponent @child-event="handleChildEvent" />
  187. </template><ChildComponent :prop-name="dynamicValue" />
  188. </template><template>
  189. <template>
  190.   <ChildComponent @child-event="handleChildEvent" />
  191. </template><ChildComponent :prop-name="dynamicValue" />
  192. </template><template>
  193. <template>
  194.   <ChildComponent @child-event="handleChildEvent" />
  195. </template><ChildComponent :prop-name="dynamicValue" />
  196. </template>type: Array,
  197. <template>
  198. <template>
  199.   <ChildComponent @child-event="handleChildEvent" />
  200. </template><ChildComponent :prop-name="dynamicValue" />
  201. </template><template>
  202. <template>
  203.   <ChildComponent @child-event="handleChildEvent" />
  204. </template><ChildComponent :prop-name="dynamicValue" />
  205. </template><template>
  206. <template>
  207.   <ChildComponent @child-event="handleChildEvent" />
  208. </template><ChildComponent :prop-name="dynamicValue" />
  209. </template>default: () => []
  210. <template>
  211. <template>
  212.   <ChildComponent @child-event="handleChildEvent" />
  213. </template><ChildComponent :prop-name="dynamicValue" />
  214. </template><template>
  215. <template>
  216.   <ChildComponent @child-event="handleChildEvent" />
  217. </template><ChildComponent :prop-name="dynamicValue" />
  218. </template>},
  219. <template>
  220. <template>
  221.   <ChildComponent @child-event="handleChildEvent" />
  222. </template><ChildComponent :prop-name="dynamicValue" />
  223. </template><template>
  224. <template>
  225.   <ChildComponent @child-event="handleChildEvent" />
  226. </template><ChildComponent :prop-name="dynamicValue" />
  227. </template>// 对象类型验证
  228. <template>
  229. <template>
  230.   <ChildComponent @child-event="handleChildEvent" />
  231. </template><ChildComponent :prop-name="dynamicValue" />
  232. </template><template>
  233. <template>
  234.   <ChildComponent @child-event="handleChildEvent" />
  235. </template><ChildComponent :prop-name="dynamicValue" />
  236. </template>config: {
  237. <template>
  238. <template>
  239.   <ChildComponent @child-event="handleChildEvent" />
  240. </template><ChildComponent :prop-name="dynamicValue" />
  241. </template><template>
  242. <template>
  243.   <ChildComponent @child-event="handleChildEvent" />
  244. </template><ChildComponent :prop-name="dynamicValue" />
  245. </template><template>
  246. <template>
  247.   <ChildComponent @child-event="handleChildEvent" />
  248. </template><ChildComponent :prop-name="dynamicValue" />
  249. </template>type: Object,
  250. <template>
  251. <template>
  252.   <ChildComponent @child-event="handleChildEvent" />
  253. </template><ChildComponent :prop-name="dynamicValue" />
  254. </template><template>
  255. <template>
  256.   <ChildComponent @child-event="handleChildEvent" />
  257. </template><ChildComponent :prop-name="dynamicValue" />
  258. </template><template>
  259. <template>
  260.   <ChildComponent @child-event="handleChildEvent" />
  261. </template><ChildComponent :prop-name="dynamicValue" />
  262. </template>default: () => ({})
  263. <template>
  264. <template>
  265.   <ChildComponent @child-event="handleChildEvent" />
  266. </template><ChildComponent :prop-name="dynamicValue" />
  267. </template><template>
  268. <template>
  269.   <ChildComponent @child-event="handleChildEvent" />
  270. </template><ChildComponent :prop-name="dynamicValue" />
  271. </template>},
  272. <template>
  273. <template>
  274.   <ChildComponent @child-event="handleChildEvent" />
  275. </template><ChildComponent :prop-name="dynamicValue" />
  276. </template><template>
  277. <template>
  278.   <ChildComponent @child-event="handleChildEvent" />
  279. </template><ChildComponent :prop-name="dynamicValue" />
  280. </template>// 布尔值类型验证
  281. <template>
  282. <template>
  283.   <ChildComponent @child-event="handleChildEvent" />
  284. </template><ChildComponent :prop-name="dynamicValue" />
  285. </template><template>
  286. <template>
  287.   <ChildComponent @child-event="handleChildEvent" />
  288. </template><ChildComponent :prop-name="dynamicValue" />
  289. </template>isActive: {
  290. <template>
  291. <template>
  292.   <ChildComponent @child-event="handleChildEvent" />
  293. </template><ChildComponent :prop-name="dynamicValue" />
  294. </template><template>
  295. <template>
  296.   <ChildComponent @child-event="handleChildEvent" />
  297. </template><ChildComponent :prop-name="dynamicValue" />
  298. </template><template>
  299. <template>
  300.   <ChildComponent @child-event="handleChildEvent" />
  301. </template><ChildComponent :prop-name="dynamicValue" />
  302. </template>type: Boolean,
  303. <template>
  304. <template>
  305.   <ChildComponent @child-event="handleChildEvent" />
  306. </template><ChildComponent :prop-name="dynamicValue" />
  307. </template><template>
  308. <template>
  309.   <ChildComponent @child-event="handleChildEvent" />
  310. </template><ChildComponent :prop-name="dynamicValue" />
  311. </template><template>
  312. <template>
  313.   <ChildComponent @child-event="handleChildEvent" />
  314. </template><ChildComponent :prop-name="dynamicValue" />
  315. </template>default: false
  316. <template>
  317. <template>
  318.   <ChildComponent @child-event="handleChildEvent" />
  319. </template><ChildComponent :prop-name="dynamicValue" />
  320. </template><template>
  321. <template>
  322.   <ChildComponent @child-event="handleChildEvent" />
  323. </template><ChildComponent :prop-name="dynamicValue" />
  324. </template>}
  325. <template>
  326. <template>
  327.   <ChildComponent @child-event="handleChildEvent" />
  328. </template><ChildComponent :prop-name="dynamicValue" />
  329. </template>}
  330. }
复制代码
在这个例子中,title 被验证为字符串类型,有一个默认值;width 被验证为数值类型,有一个默认值,并且有一个自定义的验证函数确保它非负;items 被验证为数组类型,有一个默认的空数组;config 被验证为对象类型,有一个默认的空对象;isActive 被验证为布尔类型,有一个默认的 false 值。
如果父组件传递给子组件的 props 不满足这些验证规则,Vue 将抛出一个警告。这些验证规则有助于确保组件接收到的数据是预期的类型和格式,从而提高组件的健壮性。AD:首页 | 一个覆盖广泛主题工具的高效在线平台
动态Props

在Vue中,动态Props指的是父组件向子组件传递的Props值可以在运行时动态确定。这可以通过使用v-bind指令或简写为:来实现。动态Props的值可以是表达式、计算属性或方法返回的结果。
  1. <template>
  2. <template>
  3.   <ChildComponent @child-event="handleChildEvent" />
  4. </template><ChildComponent :prop-name="dynamicValue" />
  5. </template>
复制代码
单向数据流

在Vue中,单向数据流指的是数据只能从父组件流向子组件,不能反向流动。这是通过props和emit方法实现的。props用于父组件向子组件传递数据,而emit方法允许子组件向父组件发送事件。
AD:专业搜索引擎
父子组件通信

父子组件通信主要有以下几种方式:

  • Props:父组件通过Props向子组件传递数据。
  • **\(emit 方法**:子组件通过`\)emit方法触发事件,父组件通过@eventName`监听这些事件。
  1. <template>
  2. <template>
  3.   <ChildComponent @child-event="handleChildEvent" />
  4. </template><ChildComponent :prop-name="dynamicValue" />
  5. </template>
复制代码
  1. <template>
  2. <template>
  3.   <ChildComponent @child-event="handleChildEvent" />
  4. </template><ChildComponent :prop-name="dynamicValue" />
  5. </template>发送消息给父组件
复制代码
非父子组件通信

非父子组件通信有以下几种方式:

  • Provide 和 Inject:祖先组件通过provide选项来提供变量,所有的子孙组件都可以通过inject选项来接收这个变量。
    AD:漫画首页
  • Event Bus:创建一个中央事件总线实例,不同组件通过触发或监听事件来进行通信。
  1. // 创建Event Bus实例const eventBus = new Vue();// 祖先组件eventBus.$emit('update-data', 'some data');// 后代组件eventBus.$on('update-data', (data) => {<template>
  2. <template>
  3.   <ChildComponent @child-event="handleChildEvent" />
  4. </template><ChildComponent :prop-name="dynamicValue" />
  5. </template>console.log('收到数据:', data);});
复制代码
Props 的限制


  • 类型限制:可以指定props的类型,如String、Number、Boolean等。
  • 默认值:可以为props设置默认值。
  • 必需性:可以指定props是否为必需。
  • 自定义验证:可以使用validator函数进行自定义验证。
  • 单位转换:对于数值类型,可以指定单位,如px、%等,Vue会自动进行单位转换。
总结

Vue.js通过提供灵活的组件通信机制,使得前端开发更加高效和模块化。父子组件之间的通信通过props和emit实现,遵循单向数据流原则;非父子组件间则可以通过provide和inject,或者事件总线来实现。这些通信机制不仅使得组件之间的数据传递更加清晰,也提高了应用的可维护性。同时,props的验证机制确保了组件接收到的数据是符合预期格式的,增加了组件的稳定性。

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册