当前位置:首页 > 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中实现穿梭框组件,可以使用<el-transfer>(Element UI)或手动实现。以下是手动实现的核心逻辑: <template> &l…

vue 方法实现

vue 方法实现

在 Vue 中,方法的实现通常通过 methods 选项完成。以下是 Vue 方法实现的核心要点和示例: 基本方法定义 在 Vue 组件中定义方法时,需将函数声明放在 methods 对象内。这些方…

vue实现试卷

vue实现试卷

Vue实现试卷系统的基本思路 使用Vue.js构建试卷系统需要结合组件化开发、状态管理和后端数据交互。核心功能包括题目展示、答题逻辑、计时器和提交答案。 核心功能模块划分 试卷组件结构 试卷容…

vue 实现筛选

vue 实现筛选

实现筛选功能的基本方法 在Vue中实现筛选功能通常涉及以下几个核心步骤: 数据绑定与筛选逻辑 使用v-model绑定筛选条件到Vue实例的数据属性,结合计算属性实现动态筛选: data() {…

vue 实现全屏

vue 实现全屏

Vue 实现全屏功能 在 Vue 中实现全屏功能可以通过浏览器提供的 Fullscreen API 完成。以下是一个完整的实现方案: 安装依赖(可选) 如果需要跨浏览器兼容性更好,可以安装 sc…

vue实现导入

vue实现导入

Vue 实现文件导入功能 使用 <input type="file"> 元素 在 Vue 模板中添加一个文件输入元素,绑定 change 事件处理函数。 <template>…