当前位置:首页 > VUE

vue 组件实现

2026-02-10 05:58:13VUE

Vue 组件实现基础

Vue 组件是 Vue.js 的核心特性之一,允许将 UI 拆分为独立、可复用的模块。组件可以包含模板、逻辑和样式,并通过 props 和 events 与其他组件通信。

创建组件的方式

  • 单文件组件 (SFC):使用 .vue 文件,包含 <template><script><style> 三个部分。
  • 全局注册:通过 Vue.component() 方法注册全局可用组件。
  • 局部注册:在组件选项中通过 components 属性注册局部组件。

单文件组件示例

<template>
  <div class="example">
    <p>{{ message }}</p>
    <button @click="updateMessage">Update</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      message: 'Hello Vue!'
    }
  },
  methods: {
    updateMessage() {
      this.message = 'Message updated!'
    }
  }
}
</script>

<style scoped>
.example {
  color: blue;
}
</style>

组件通信

Props 传递数据 父组件通过 props 向子组件传递数据,子组件通过 props 选项接收。

vue 组件实现

<!-- ParentComponent.vue -->
<template>
  <ChildComponent :title="parentTitle" />
</template>

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

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

<!-- ChildComponent.vue -->
<template>
  <h2>{{ title }}</h2>
</template>

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

Events 触发动作 子组件通过 $emit 触发事件,父组件通过 v-on 监听。

<!-- ChildComponent.vue -->
<template>
  <button @click="$emit('custom-event', 'payload')">Click me</button>
</template>

<!-- ParentComponent.vue -->
<template>
  <ChildComponent @custom-event="handleEvent" />
</template>

<script>
export default {
  methods: {
    handleEvent(payload) {
      console.log(payload) // 'payload'
    }
  }
}
</script>

插槽 (Slots)

插槽允许父组件向子组件注入内容。

vue 组件实现

<!-- ChildComponent.vue -->
<template>
  <div>
    <slot></slot>
  </div>
</template>

<!-- ParentComponent.vue -->
<template>
  <ChildComponent>
    <p>Content passed from parent</p>
  </ChildComponent>
</template>

生命周期钩子

Vue 组件提供多个生命周期钩子,用于在不同阶段执行代码。

export default {
  created() {
    // 组件实例创建后调用
  },
  mounted() {
    // DOM 挂载后调用
  },
  updated() {
    // 数据更新导致 DOM 重新渲染后调用
  },
  destroyed() {
    // 组件销毁后调用
  }
}

动态组件

通过 <component :is="currentComponent"> 实现动态切换组件。

<template>
  <component :is="currentComponent"></component>
  <button @click="toggleComponent">Toggle</button>
</template>

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

export default {
  data() {
    return {
      currentComponent: 'ComponentA'
    }
  },
  components: { ComponentA, ComponentB },
  methods: {
    toggleComponent() {
      this.currentComponent = this.currentComponent === 'ComponentA' 
        ? 'ComponentB' 
        : 'ComponentA'
    }
  }
}
</script>

组件最佳实践

  • 保持组件单一职责,每个组件只做一件事。
  • 合理拆分大型组件为多个小型组件。
  • 使用有意义的组件命名,遵循 PascalCase 命名约定。
  • 为 props 定义明确的类型和默认值。
  • 使用 scoped CSS 避免样式污染。

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

相关文章

vue 实现$(id)

vue 实现$(id)

在 Vue 中实现类似 jQuery 的 $(id) 功能,可以通过以下几种方式操作 DOM 元素: 使用 ref 属性 Vue 推荐使用 ref 属性来直接访问 DOM 元素或组件实例。在模板中为…

vue实现门户

vue实现门户

Vue 实现门户网站的关键步骤 搭建基础框架 使用 Vue CLI 或 Vite 初始化项目,安装 Vue Router 管理多页面路由。门户通常需要响应式布局,可引入 UI 库如 Element P…

vue日历实现

vue日历实现

实现基础日历布局 使用Vue的模板语法构建日历的基本HTML结构。一个典型的日历包含星期标题和日期格子,可以通过v-for循环生成。月份切换功能通过计算属性动态更新日期数组。 <templat…

vue实现滚动翻页

vue实现滚动翻页

vue实现滚动翻页的方法 监听滚动事件 在Vue组件中,通过@scroll或window.addEventListener监听滚动事件,判断是否滚动到底部。使用clientHeight、scrollT…

vue制作css

vue制作css

在Vue中制作CSS的方法 Vue提供了多种灵活的方式来编写和管理CSS,可以根据项目需求选择适合的方法。 使用内联样式 内联样式可以直接在Vue组件的模板中编写,适用于简单的样式需求。 <…

vue实现原理

vue实现原理

Vue 实现原理 Vue.js 的核心实现原理主要包括响应式系统、虚拟 DOM、模板编译和组件化机制。以下是关键实现细节的分解: 响应式系统 Vue 2.x 使用 Object.defineProp…