当前位置:首页 > 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 简化父子组件通信。

vue父子实现

子组件需要接收 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 事件:

vue父子实现

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 实现看板功能可以通过组件化开发、状态管理和拖拽库结合完成。以下是一个完整的实现方案: 基础项目结构 src/ ├── components/ │ ├── Ka…

vue实现ssr

vue实现ssr

Vue 实现 SSR(服务端渲染) Vue 的服务端渲染(SSR)允许在服务器端生成完整的 HTML 内容,再发送到客户端,提升首屏加载速度和 SEO 友好性。以下是实现 Vue SSR 的关键步骤和…

vue实现CRUD

vue实现CRUD

Vue 实现 CRUD 操作 Vue 提供了响应式数据绑定和组件化开发能力,结合后端 API 可以轻松实现 CRUD(增删改查)功能。以下是基于 Vue 3 和 Axios 的完整实现方案。 安装依…

vue指令实现

vue指令实现

Vue 指令的实现方式 Vue 指令是特殊的标记,用于在 DOM 元素上应用特定的行为。可以通过全局或局部方式注册自定义指令。 全局注册指令 在 Vue 应用中通过 Vue.directive 方法…

vue实现刷新

vue实现刷新

Vue 实现页面刷新的方法 在Vue中实现页面刷新可以通过多种方式,以下是几种常见的方法: 使用 window.location.reload() 通过调用浏览器的原生方法强制刷新当前页面:…

vue实现联动

vue实现联动

Vue 实现联动效果 联动效果通常指多个组件或表单元素之间相互影响,例如选择省份后动态加载城市列表。Vue 提供了多种方式实现联动,包括数据绑定、计算属性、侦听器等。 数据驱动联动 通过 Vue 的…