当前位置:首页 > 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 实现适老化样式的关键方法 全局字体与字号调整 通过 CSS 变量或主题配置统一放大基础字号,建议正文不小于 18px,标题更大。在 App.vue 中设置全局样式: :root { -…

vue路由实现内部切换

vue路由实现内部切换

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

vue实现triger

vue实现triger

在Vue中实现触发器(trigger)功能通常涉及自定义事件或DOM事件触发。以下是几种常见场景的实现方法: 自定义事件触发 通过$emit方法触发父组件中监听的自定义事件: // 子组件 t…

vue实现画图

vue实现画图

Vue 实现画图功能 在 Vue 中实现画图功能通常需要结合 HTML5 的 <canvas> 元素或第三方库。以下是几种常见的方法: 使用原生 Canvas API 通过 Vue 直接…

vue 全景实现

vue 全景实现

Vue 全景实现方案 在Vue中实现全景效果,通常需要结合WebGL或第三方库来处理3D场景渲染。以下是几种常见方法: 使用Three.js库 Three.js是一个强大的WebGL库,适合创建3D…

vue实现webapp

vue实现webapp

Vue 实现 WebApp 的基本步骤 环境准备 确保已安装 Node.js 和 npm/yarn。通过 Vue CLI 或 Vite 创建项目: npm install -g @vue/cli…