当前位置:首页 > VUE

vue实现组件

2026-02-10 16:34:34VUE

Vue 实现组件的方法

单文件组件(SFC)

使用 .vue 文件格式,包含 <template><script><style> 三个部分,是 Vue 官方推荐的组件开发方式。

<template>
  <div class="example">{{ msg }}</div>
</template>

<script>
export default {
  data() {
    return {
      msg: 'Hello Vue!'
    }
  }
}
</script>

<style>
.example {
  color: red;
}
</style>

选项式 API

通过 Vue.component() 全局注册或组件选项对象局部注册,适合 Vue 2 或偏好选项式开发的场景。

// 全局组件
Vue.component('button-counter', {
  data() {
    return {
      count: 0
    }
  },
  template: '<button @click="count++">Clicked {{ count }} times</button>'
})

// 局部组件
const ComponentA = {
  template: '<div>Component A</div>'
}
new Vue({
  components: {
    'component-a': ComponentA
  }
})

组合式 API(Vue 3)

使用 setup() 函数和响应式 API(如 refreactive),更适合复杂逻辑的封装和复用。

<script setup>
import { ref } from 'vue'

const count = ref(0)
</script>

<template>
  <button @click="count++">Count: {{ count }}</button>
</template>

函数式组件

无状态、无实例的轻量级组件,通过渲染函数实现,适用于纯展示型场景。

Vue.component('functional-button', {
  functional: true,
  render(createElement, context) {
    return createElement('button', context.data, context.children)
  }
})

动态组件

通过 <component :is> 实现运行时动态切换组件,常用于标签页或条件渲染。

<template>
  <component :is="currentComponent"></component>
</template>

<script>
import ComponentA from './ComponentA.vue'
import ComponentB from './ComponentB.vue'

export default {
  data() {
    return {
      currentComponent: 'ComponentA'
    }
  },
  components: { ComponentA, ComponentB }
}
</script>

异步组件

使用 defineAsyncComponent 或动态 import() 实现按需加载,优化应用性能。

// Vue 3
const AsyncComponent = defineAsyncComponent(() =>
  import('./AsyncComponent.vue')
)

// Vue 2
Vue.component('async-component', () => import('./AsyncComponent.vue'))

递归组件

组件调用自身时需要设置 name 选项,适用于树形结构等嵌套场景。

<template>
  <div>
    {{ data.name }}
    <tree-item v-for="child in data.children" :data="child" :key="child.id"/>
  </div>
</template>

<script>
export default {
  name: 'TreeItem',
  props: ['data']
}
</script>

组件通信方式

  • Props / Events:父子组件通过 props 向下传递数据,通过 $emit 向上触发事件
  • Provide / Inject:祖先组件向后代组件跨层级提供数据
  • Vuex / Pinia:状态管理库解决复杂组件通信
  • Event Bus:通过空的 Vue 实例作为事件中心(小型项目适用)
  • $attrs / $listeners:处理非 props 的特性和事件(Vue 2)
  • v-model 双向绑定:通过 modelValue prop 和 update:modelValue 事件实现

生命周期钩子

组件在不同阶段会触发特定钩子,可用于执行初始化或清理操作:

  • beforeCreate / created
  • beforeMount / mounted
  • beforeUpdate / updated
  • beforeUnmount / unmounted(Vue 3)
  • errorCaptured

最佳实践

  • 组件命名采用 PascalCase(单文件组件)或 kebab-case(DOM 模板)
  • 单一职责原则,保持组件功能聚焦
  • 合理划分容器组件与展示组件
  • Prop 定义时指定类型和默认值
  • 为列表渲染的组件设置唯一 key
  • 复杂逻辑考虑使用自定义 Hook(Vue 3)或 Mixin(Vue 2)

vue实现组件

标签: 组件vue
分享给朋友:

相关文章

vue实现文字播放栏

vue实现文字播放栏

Vue 实现文字播放栏(跑马灯效果) 方法一:使用 CSS 动画 + Vue 数据绑定 通过 CSS 的 @keyframes 实现动画效果,结合 Vue 的动态数据绑定控制内容。 <tem…

vue实现活动倒计时

vue实现活动倒计时

实现活动倒计时的基本思路 在Vue中实现活动倒计时通常需要获取当前时间与活动结束时间的差值,并将其转换为天、小时、分钟、秒的格式展示。通过定时器每秒更新显示的时间。 定义倒计时数据 在Vue组件的d…

vue实现下拉框删除

vue实现下拉框删除

Vue 实现下拉框删除功能 在Vue中实现下拉框的删除功能通常涉及以下步骤: 数据绑定与渲染 在Vue组件中定义数据数组用于存储下拉框选项: data() { return {…

vue实现星星

vue实现星星

实现星星评分的Vue组件 使用Vue实现星星评分功能可以通过组件化方式完成,以下是一个完整的实现方案: 模板部分 <template> <div class="star-ra…

vue实现rtc

vue实现rtc

Vue 实现 RTC 的步骤 安装依赖 在 Vue 项目中安装 WebRTC 相关依赖,如 simple-peer 或 peerjs,用于简化 WebRTC 的实现。 运行以下命令安装 simple-…

vue原理实现

vue原理实现

Vue 原理实现的核心机制 Vue.js 的核心原理基于响应式系统、虚拟 DOM 和模板编译。以下是其核心实现机制的分解: 响应式系统 Vue 使用 Object.defineProperty(V…