当前位置:首页 > VUE

vue实现组件通信

2026-03-28 16:26:32VUE

父子组件通信

父组件通过 props 向子组件传递数据,子组件通过 $emit 触发事件向父组件传递数据。

父组件:

<template>
  <ChildComponent :message="parentMessage" @childEvent="handleChildEvent" />
</template>

<script>
import ChildComponent from './ChildComponent.vue';

export default {
  components: { ChildComponent },
  data() {
    return {
      parentMessage: 'Hello from parent'
    };
  },
  methods: {
    handleChildEvent(data) {
      console.log('Received from child:', data);
    }
  }
};
</script>

子组件:

<template>
  <div>
    <p>{{ message }}</p>
    <button @click="sendToParent">Send to Parent</button>
  </div>
</template>

<script>
export default {
  props: ['message'],
  methods: {
    sendToParent() {
      this.$emit('childEvent', { data: 'Hello from child' });
    }
  }
};
</script>

兄弟组件通信

通过共同的父组件作为桥梁,或者使用事件总线、Vuex 等状态管理工具。

使用事件总线:

// event-bus.js
import Vue from 'vue';
export const EventBus = new Vue();

组件A:

import { EventBus } from './event-bus.js';

EventBus.$emit('eventName', data);

组件B:

import { EventBus } from './event-bus.js';

EventBus.$on('eventName', (data) => {
  console.log(data);
});

跨层级组件通信

使用 provide/inject 实现祖先组件向后代组件传递数据。

祖先组件:

export default {
  provide() {
    return {
      sharedData: 'Shared value'
    };
  }
};

后代组件:

export default {
  inject: ['sharedData'],
  mounted() {
    console.log(this.sharedData);
  }
};

Vuex 状态管理

适用于复杂应用中的全局状态管理。

vue实现组件通信

store.js:

import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    increment(state) {
      state.count++;
    }
  },
  actions: {
    increment({ commit }) {
      commit('increment');
    }
  }
});

组件中使用:

export default {
  computed: {
    count() {
      return this.$store.state.count;
    }
  },
  methods: {
    increment() {
      this.$store.dispatch('increment');
    }
  }
};

使用 $refs 直接访问组件

父组件可以通过 ref 直接访问子组件的属性和方法。

父组件:

<template>
  <ChildComponent ref="child" />
</template>

<script>
export default {
  mounted() {
    this.$refs.child.childMethod();
  }
};
</script>

使用 $attrs 和 $listeners

用于传递未被 props 识别的特性和事件。

子组件:

vue实现组件通信

<template>
  <div v-bind="$attrs" v-on="$listeners"></div>
</template>

使用 v-model 实现双向绑定

自定义组件支持 v-model

子组件:

<template>
  <input :value="value" @input="$emit('input', $event.target.value)" />
</template>

<script>
export default {
  props: ['value']
};
</script>

父组件:

<template>
  <ChildComponent v-model="message" />
</template>

使用 .sync 修饰符

实现多个 props 的双向绑定:

父组件:

<ChildComponent :title.sync="pageTitle" />

子组件:

this.$emit('update:title', newTitle);

标签: 组件通信
分享给朋友:

相关文章

react如何开发组件

react如何开发组件

React 组件开发基础 React 组件分为函数组件和类组件两种形式。函数组件是现代 React 开发的主流方式,结合 Hooks 可以实现完整功能。 函数组件示例: function Gree…

vue 实现组件刷新

vue 实现组件刷新

组件局部刷新 在Vue中实现组件刷新可以通过强制重新渲染组件来实现。常用的方法有以下几种: 使用v-if指令 通过切换v-if条件触发组件的销毁和重建 <template> <…

vue实现组件通信

vue实现组件通信

Vue 组件通信方法 Vue 提供了多种方式实现组件间的通信,适用于不同场景。以下是常见的方法: Props 和 Events 父组件通过 props 向子组件传递数据,子组件通过 $emit 触…

vue实现盖章组件

vue实现盖章组件

实现Vue盖章组件的步骤 组件结构设计 创建一个Vue组件,包含画布元素和必要的控制按钮。画布用于显示盖章效果,按钮用于触发盖章操作。组件需要接收图片和盖章位置等参数。 <template&g…

vue实现路由组件

vue实现路由组件

Vue 实现路由组件的方法 在 Vue 中实现路由功能通常需要结合 Vue Router 库,以下是具体实现步骤: 安装 Vue Router 通过 npm 或 yarn 安装 Vue Router…

vue实现组件缓存

vue实现组件缓存

Vue 组件缓存的实现方法 在 Vue 中实现组件缓存通常使用 <keep-alive> 内置组件,它可以缓存不活动的组件实例,避免重复渲染和销毁。 基本用法 <keep-aliv…