当前位置:首页 > VUE

vue父子实现

2026-01-17 17:37:08VUE

Vue 父子组件通信实现

Vue 中父子组件通信主要通过 props 和自定义事件实现。父组件向子组件传递数据使用 props,子组件向父组件传递数据使用自定义事件。

父组件向子组件传递数据

父组件通过 props 向子组件传递数据。子组件需要在 props 选项中声明接收的属性。

父组件模板:

<template>
  <child-component :message="parentMessage" />
</template>

父组件脚本:

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

export default {
  components: {
    ChildComponent
  },
  data() {
    return {
      parentMessage: 'Hello from parent'
    }
  }
}
</script>

子组件脚本:

<script>
export default {
  props: {
    message: {
      type: String,
      required: true
    }
  }
}
</script>

子组件向父组件传递数据

子组件通过 $emit 触发自定义事件向父组件传递数据。父组件通过 v-on 监听子组件触发的事件。

子组件模板:

<template>
  <button @click="sendMessage">Send Message</button>
</template>

子组件脚本:

<script>
export default {
  methods: {
    sendMessage() {
      this.$emit('message-from-child', 'Hello from child')
    }
  }
}
</script>

父组件模板:

<template>
  <child-component @message-from-child="handleChildMessage" />
</template>

父组件脚本:

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

export default {
  components: {
    ChildComponent
  },
  methods: {
    handleChildMessage(message) {
      console.log(message) // 输出: 'Hello from child'
    }
  }
}
</script>

使用 v-model 实现双向绑定

对于表单元素等需要双向绑定的场景,可以使用 v-model 简化父子组件通信。

子组件需要接收 value prop 并触发 input 事件:

子组件模板:

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

子组件脚本:

<script>
export default {
  props: {
    value: {
      type: String,
      required: true
    }
  }
}
</script>

父组件使用:

<template>
  <child-component v-model="parentValue" />
</template>

使用 .sync 修饰符

Vue 2.x 中可以使用 .sync 修饰符实现父子组件双向绑定。

子组件触发 update:propName 事件:

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

父组件使用:

<child-component :title.sync="parentTitle" />

访问子组件实例

父组件可以通过 ref 直接访问子组件实例:

父组件模板:

<template>
  <child-component ref="child" />
</template>

父组件脚本:

<script>
export default {
  methods: {
    callChildMethod() {
      this.$refs.child.childMethod()
    }
  }
}
</script>

插槽通信

通过作用域插槽可以实现父组件访问子组件数据:

子组件模板:

<template>
  <div>
    <slot :user="user"></slot>
  </div>
</template>

父组件使用:

<template>
  <child-component>
    <template v-slot:default="slotProps">
      {{ slotProps.user.name }}
    </template>
  </child-component>
</template>

vue父子实现

标签: 父子vue
分享给朋友:

相关文章

vue实现多级弹窗

vue实现多级弹窗

Vue 多级弹窗实现方法 方法一:使用动态组件与递归组件 在 Vue 中可以通过动态组件配合递归组件实现多级弹窗。定义一个基础弹窗组件,通过递归调用自身实现层级嵌套。 基础弹窗组件模板示例:…

vue实现前端注册

vue实现前端注册

Vue 实现前端注册功能 注册表单设计 使用 Vue 的模板语法创建注册表单,包含用户名、邮箱、密码和确认密码字段。表单需绑定 v-model 实现双向数据绑定。 <template>…

vue路由实现内部切换

vue路由实现内部切换

Vue 路由实现内部切换的方法 Vue Router 是 Vue.js 官方的路由管理器,用于实现单页应用(SPA)的页面切换。以下是几种常见的内部路由切换方式: 使用 <router-lin…

vue优秀实现

vue优秀实现

Vue.js 优秀实现方法 响应式数据管理 使用 Vue 的 ref 和 reactive 处理基础类型和对象类型数据,结合 computed 计算属性优化渲染性能。对于复杂状态,推荐 Pinia 替…

eventbus vue实现

eventbus vue实现

EventBus 实现原理 EventBus 是一种发布/订阅模式,用于 Vue 组件间的通信。核心是通过一个中央事件总线管理事件监听和触发。 创建 EventBus 在 Vue 项目中创建一个独立…

vue 路由 实现

vue 路由 实现

Vue 路由实现方法 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是实现 Vue 路由的详细步骤: 安装 Vue Router 通过 npm 或 y…