• vue组件间通信的四种方法


    0 四种方式

    1. 父子传参
    2. 事件总线 bus
    3. provided/injected
    4. Vuex

    1 父子组件通信

    父传子props, 子传父$emit

    2 兄弟

    通过父组件转发

    老大->父组件->老二 (单向)

    • One.vue 老大提交自定义事件
    1. <template>
    2. <div>
    3. 老大子组件:
    4. <input type="text" v-model="msg" />
    5. <button @click="$emit('send',msg)">发送button>
    6. div>
    7. template>
    8. <script>
    9. export default {
    10. name: 'One',
    11. data() {
    12. return { msg: '' }
    13. },
    14. }
    15. script>
    • Two.vue 老二接收参数
    1. <template>
    2. <div>老二子组件: {{ msg }}div>
    3. template>
    4. <script>
    5. export default {
    6. name: 'Two',
    7. props: {
    8. msg: { type: String },
    9. },
    10. }
    11. script>
    • App.vue 父组件监听自定义事件
    1. <template>
    2. <div id="app">
    3. <one @send="handleSend">one>
    4. <two :msg="msg">two>
    5. div>
    6. template>
    7. <script>
    8. import One from './components/One.vue'
    9. import Two from './components/Two.vue'
    10. export default {
    11. name: 'App',
    12. components: {
    13. One,
    14. Two,
    15. },
    16. data() {
    17. return {
    18. msg: '',
    19. }
    20. },
    21. methods: {
    22. handleSend(msg) {
    23. this.msg = msg
    24. },
    25. },
    26. }
    27. script>

    3 表兄弟

    3.1 介绍

    全局事件总线bus (适合两个组件间通信, 没有明显关系的两个组件)

    所有的组件都通过一个统一的全局对象通信

    多个组件, 用vuex

    3.2 事件总线用法

    • bus/index.js
    1. import Vue from 'vue'
    2. // 这是一个Vue实例对象
    3. const bus = new Vue()
    4. export default bus
    •  Counter.vue 发
    1. <template>
    2. <div>
    3. <button @click="handleClick">向Test发送消息button>
    4. div>
    5. template>
    6. <script>
    7. import bus from '@/bus'
    8. export default {
    9. name: 'Counter',
    10. data() {
    11. return {
    12. msg: '',
    13. }
    14. },
    15. methods: {
    16. handleClick() {
    17. // 在一个组件中触发事件
    18. bus.$emit('send', this.msg)
    19. },
    20. },
    21. }
    22. script>
    •  Test.vue 收
    1. <template>
    2. <div>
    3. 从counter中收到的消息 {{ msg }}
    4. div>
    5. template>
    6. <script>
    7. import bus from '@/bus'
    8. export default {
    9. name: 'Test',
    10. data() { return { msg: '' } },
    11. mounted() {
    12. // 监听自定义事件
    13. bus.$on('send', (value) => {
    14. this.msg = value
    15. })
    16. },
    17. beforeDestroy() {
    18. // 移除自定义事件
    19. bus.$off('send')
    20. },
    21. }
    22. script>

    4 祖孙组件通信

    依赖注入 provided/injected (vue3直接是响应式)

    1) 传静态数据

            1) 在祖先组件中通过 provid选项提供数据

            2) 在后代组件中通过injection选项注入数据

    •  App.vue
    1. // 父级组件提供 'foo'
    2. export default {
    3. name: 'App',
    4. provide: {
    5. foo: 'hello'
    6. },
    7. // ...
    8. }
    • son.vue 
    1. // 孙子组件注入 'foo'
    2. export default {
    3. name: 'Son',
    4. inject: ['foo'],
    5. created () {
    6. console.log(this.foo)
    7. }
    8. }

    2) 传响应式数据

    传入对象

    • App.vue 
    1. <template>
    2. <div id="app">
    3. <input type="text" v-model="obj.msg" />
    4. div>
    5. template>
    6. <script>
    7. export default {
    8. name: 'App',
    9. provide() {
    10. return { foo: this.obj }
    11. },
    12. data() { return { obj: { msg: '' } } },
    13. }
    14. script>
    • son.vue 
    1. <template>
    2. <div>{{ foo.msg }}div>
    3. template>
    4. <script>
    5. export default {
    6. name: 'Son',
    7. inject: ['foo'],
    8. }
    9. script>

    传入函数

    •  App.vue
    1. <template>
    2. <div id="app">
    3. <input type="text" v-model="msg" />
    4. <counter>counter>
    5. <test>test>
    6. div>
    7. template>
    8. <script>
    9. export default {
    10. name: 'App',
    11. provide() {
    12. // provide设置为一个函数, 该函数中this指向vm实例
    13. return { foo: () => this.msg }
    14. },
    15. data() { return { msg: '' } },
    16. }
    17. script>
    •  son.vue
    1. <template>
    2. <div>{{ foo() }}div>
    3. template>
    4. <script>
    5. export default {
    6. name: 'Son',
    7. inject: ['foo'],
    8. }
    9. script>

  • 相关阅读:
    一个优秀的软件测试工程师该如何进行需求分析
    ActiveReportsJS:How to Add Angular Report Viewer Web App
    探秘公有IP地址与私有IP地址的区别及其在路由控制中的作用
    neo4j数据库导出
    使用 Burpsuite 与 xray 进行联动
    【软考 系统架构设计师】嵌入式系统④ 嵌入式操作系统
    华为HMS Core携手超图为三维GIS注入新动能
    [LeetCode]剑指 Offer 14- II. 剪绳子 II
    C++内存管理(3)——内存池
    nginx
  • 原文地址:https://blog.csdn.net/weixin_43895819/article/details/127713916