Vue组件通信 (六)

- 日理万妓 2022-12-13 01:37 291阅读 0赞

目录

    1. 父向子组件通信
    • 1.1 props
    • 1.2 ref
    • 1.3 children
    1. 子向父组件通信
    • 2.1 emit
  • 3 子向子组件通信
    • 3.1 parent
  • 4 父向孙传值
    • 4.1 provide inject
  • 5 任意两个组件之间
    • 5.1 eventBus
    1. vuex

组件通信是vue中很常见也是很重要的一部分。

1. 父向子组件通信

1.1 props

props是最常用的方式。

  1. // 父
  2. <template>
  3. <child-component1 :name="name"></child-component1>
  4. </template>
  5. <script>
  6. import ChildComponent1 from '@/components/ChildComponent1.vue'
  7. export default {
  8. components: {
  9. ChildComponent1
  10. },
  11. data () {
  12. return {
  13. name: '小明'
  14. }
  15. },
  16. }
  17. </script>
  18. // 子
  19. <template>
  20. <div>姓名:{ { name }}</div>
  21. </template>
  22. <script>
  23. export default {
  24. props: {
  25. name: {
  26. type: String, // String、Number、Boolean、Array、Object、Date、Function、Symbol
  27. default: '', // 默认值
  28. required: true, // 定义该 prop 是否是必填项。如果为true未传值则会抛出警告
  29. validatorFunction, // 自定义验证参数,如果函数返回false则会抛出警告
  30. }
  31. },
  32. }
  33. </script>

props简单语法

  1. props: ['name']

props只定义字符串类型

  1. props: {
  2. name: String
  3. }

props如果参数为多类型

  1. props: {
  2. name: ['String', 'Number']
  3. }

1.2 ref

这种方式比较少用到。

  1. // 父
  2. <template>
  3. <div class="page">
  4. <child-component1 :name="name" ref="cp"></child-component1>
  5. </div>
  6. </template>
  7. <script>
  8. import ChildComponent1 from '@/components/ChildComponent1.vue'
  9. export default {
  10. components: {
  11. ChildComponent1
  12. },
  13. data () {
  14. return {
  15. name: '小明'
  16. }
  17. },
  18. mounted () {
  19. this.$refs.cp.age = 18
  20. }
  21. }
  22. </script>
  23. // 子
  24. <template>
  25. <div class="container">
  26. <div>姓名:{ { name }}</div>
  27. <div>年龄:{ { age }}</div>
  28. </div>
  29. </template>
  30. <script>
  31. export default {
  32. props: {
  33. name: {
  34. type: String,
  35. default: ''
  36. }
  37. },
  38. data () {
  39. return {
  40. age: 0
  41. }
  42. }
  43. }
  44. </script>

1.3 children

通过父组件的children拿到实例,children里是父组件中子组件的集合,官方文档说了不保证里面的顺序。

  1. this.$children[0].age = 30

2. 子向父组件通信

2.1 emit

通过子组件派发事件,父组件监听事件来传值。

  1. // 父
  2. <template>
  3. <div class="page">
  4. <child-component1 @onChildComponen="onChildComponen"></child-component1>
  5. </div>
  6. </template>
  7. <script>
  8. import ChildComponent1 from '@/components/ChildComponent1.vue'
  9. export default {
  10. components: {
  11. ChildComponent1
  12. },
  13. methods: {
  14. onChildComponen (data) {
  15. alert(data)
  16. }
  17. }
  18. }
  19. </script>
  20. // 子
  21. <template>
  22. <div class="container">
  23. <div @click="onClick">emit</div>
  24. </div>
  25. </template>
  26. <script>
  27. export default {
  28. methods: {
  29. onClick () {
  30. this.$emit('onChildComponen', 1)
  31. }
  32. }
  33. }
  34. </script>

3 子向子组件通信

他们需要有一个共同的父组件,通过$parent.$emit来派发事件,再通过$parent.$on监听事件

3.1 parent

  1. // 父
  2. <template>
  3. <div class="page">
  4. <child-component1 ></child-component1>
  5. <child-component2 ></child-component2>
  6. </div>
  7. </template>
  8. <script>
  9. import ChildComponent1 from '@/components/ChildComponent1.vue'
  10. import ChildComponent2 from '@/components/ChildComponent2.vue'
  11. export default {
  12. components: {
  13. ChildComponent1,
  14. ChildComponent2
  15. }
  16. }
  17. </script>
  18. // 子1
  19. <template>
  20. <div class="container">
  21. <div @click="onClick">子组件1</div>
  22. </div>
  23. </template>
  24. <script>
  25. export default {
  26. created () {
  27. },
  28. methods: {
  29. onClick () {
  30. this.$parent.$emit('hi', '你好')
  31. }
  32. }
  33. }
  34. </script>
  35. // 子2
  36. <template>
  37. <div class="container">
  38. <div>子组件2</div>
  39. </div>
  40. </template>
  41. <script>
  42. export default {
  43. created () {
  44. this.$parent.$on('hi', (data) => {
  45. alert(data)
  46. })
  47. },
  48. methods: { }
  49. }
  50. </script>

4 父向孙传值

4.1 provide inject

当组件层级过多的时候使用。
父组件提供provide,其他组件通过inject获得值,常用于组件库的发开。

  1. // 父
  2. provide() {
  3. return {
  4. name: '小明'
  5. }
  6. }
  7. // 子
  8. inject: ['name']

5 任意两个组件之间

5.1 eventBus

通过eventBus的方式,引用相同的bus来通信

  1. // bus.js
  2. import Vue from 'vue'
  3. const Bus = new Vue()
  4. export default Bus
  5. // 组件1
  6. <template>
  7. <div class="container">
  8. <div @click="onClick">子组件1</div>
  9. </div>
  10. </template>
  11. <script>
  12. import Bus from '@/bus.js'
  13. export default {
  14. created () {
  15. },
  16. methods: {
  17. onClick () {
  18. Bus.$emit('hi', '你好')
  19. }
  20. }
  21. }
  22. </script>

6. vuex

Vuex 的状态存储是响应式的。当 Vue 组件从 store 中读取状态的时候,若 store 中的状态发生变化,那么相应的组件也会相应地得到高效更新。

发表评论

表情:
评论列表 (有 0 条评论,291人围观)

还没有评论,来说两句吧...

相关阅读

    相关 VUE通信bus

    一、语法: 用于组件通信,这两个组件可以是父子关系、兄弟关系,也可以没有任何关系。bus使用$emit, $on, $off 分别来分发、监听、取消监听事件。 使用方法:

    相关 vue通信

    对于vue来说,组件之间的消息传递是非常重要的,下面是我对组件之间消息传递的常用方式的总结。 props和$emit(常用) $attrs和$listeners