当前位置:首页 > 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 选项接收。

<!-- 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)

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

<!-- 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"> 实现动态切换组件。

vue 组件实现

<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如何实现冒泡

vue如何实现冒泡

Vue 实现冒泡排序 在 Vue 中实现冒泡排序,可以通过数据绑定和响应式特性动态展示排序过程。以下是一个完整的实现示例,包含代码和详细说明。 数据准备 在 Vue 的 data 中定义一个数组作为…

实现 vue ssr

实现 vue ssr

Vue SSR 实现方法 Vue SSR(Server-Side Rendering)通过服务器端渲染 Vue 应用,提升首屏加载速度和 SEO 友好性。以下是核心实现方法: 基础配置 安装必要依赖…

vue 实现tabs

vue 实现tabs

Vue 实现 Tabs 组件的方法 使用动态组件和 v-for 在 Vue 中实现 Tabs 可以通过动态组件和 v-for 指令结合完成。定义一个 tabs 数组,包含每个标签的标题和对应的组件。…

vue实现机制

vue实现机制

Vue 实现机制的核心原理 Vue.js 的核心实现机制主要基于响应式系统、虚拟 DOM 和组件化设计。以下是关键实现细节: 响应式系统 Vue 通过 Object.defineProperty(V…

vue cli 实现

vue cli 实现

Vue CLI 实现步骤 安装 Vue CLI 确保 Node.js 版本在 8.9 或以上。通过 npm 全局安装 Vue CLI: npm install -g @vue/cli 创建新项目 使…

vue实现横滚

vue实现横滚

Vue 实现横滚效果 横滚效果通常指水平滚动内容,常见于轮播图、横向导航栏等场景。以下是几种实现方式: 使用 CSS 和 Vue 结合 通过 CSS 的 overflow-x 和 white-spa…