当前位置:首页 > VUE

vue 组件实现

2026-03-27 15:23:37VUE

Vue 组件实现

Vue 组件是 Vue.js 的核心概念之一,允许开发者将 UI 拆分为独立、可复用的模块。以下是实现 Vue 组件的常见方法:

单文件组件 (SFC)

单文件组件是 Vue 中最常用的组件形式,以 .vue 为扩展名,包含模板、脚本和样式三部分。

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

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

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

全局注册组件

通过 Vue.component 方法全局注册组件,可以在任何地方使用。

Vue.component('my-component', {
  template: '<div>A custom component!</div>'
});

局部注册组件

在组件选项中通过 components 属性局部注册组件,仅在当前组件内可用。

const ChildComponent = {
  template: '<div>A child component!</div>'
};

new Vue({
  components: {
    'child-component': ChildComponent
  }
});

动态组件

使用 <component> 标签和 is 属性实现动态组件切换。

vue 组件实现

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

函数式组件

适用于无状态、无实例的组件,通过 functional: true 声明。

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

插槽 (Slots)

通过插槽实现内容分发,增强组件的灵活性。

<template>
  <div class="container">
    <slot name="header"></slot>
    <slot></slot>
    <slot name="footer"></slot>
  </div>
</template>

Props 数据传递

父组件通过 props 向子组件传递数据。

vue 组件实现

<template>
  <child-component :message="parentMessage"></child-component>
</template>

<script>
export default {
  data() {
    return {
      parentMessage: 'Hello from parent'
    }
  }
}
</script>

自定义事件

子组件通过 $emit 触发事件,父组件监听事件。

<!-- 子组件 -->
<template>
  <button @click="$emit('custom-event', payload)">Click me</button>
</template>

<!-- 父组件 -->
<template>
  <child-component @custom-event="handleEvent"></child-component>
</template>

生命周期钩子

组件在不同阶段会触发生命周期钩子,可用于执行特定逻辑。

export default {
  created() {
    console.log('Component created');
  },
  mounted() {
    console.log('Component mounted');
  }
}

混入 (Mixins)

通过混入复用组件逻辑。

const myMixin = {
  created() {
    this.hello();
  },
  methods: {
    hello() {
      console.log('Hello from mixin!');
    }
  }
};

export default {
  mixins: [myMixin]
}

组合式 API (Vue 3)

Vue 3 的组合式 API 提供更灵活的代码组织方式。

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

const count = ref(0);

function increment() {
  count.value++;
}
</script>

<template>
  <button @click="increment">{{ count }}</button>
</template>

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

相关文章

vue评分实现

vue评分实现

Vue 评分组件实现方法 使用第三方库(如 Element UI) 安装 Element UI: npm install element-ui 引入并注册组件: import Vue from '…

vue 动画 实现

vue 动画 实现

vue 动画实现方法 Vue 提供了多种方式实现动画效果,主要包括过渡动画和动态动画两类。 使用 <transition> 组件实现基础过渡 通过 Vue 内置的 <transit…

vue实现颜色

vue实现颜色

Vue 实现颜色的方法 在 Vue 中实现颜色管理可以通过多种方式,以下是一些常见的方法: 使用 CSS 变量动态绑定颜色 在 Vue 组件的样式中定义 CSS 变量,并通过 Vue 的数据绑定动态…

vue多选实现

vue多选实现

Vue 多选实现方法 基础实现(v-model + multiple) 在 Vue 中实现多选可以通过 <select> 元素的 multiple 属性结合 v-model 完成。v-mo…

vue搜索实现

vue搜索实现

Vue 搜索功能实现方法 基于计算属性的搜索 在 Vue 中可以通过计算属性实现简单的搜索功能。这种方式适合小型数据集,利用 JavaScript 的 filter 方法进行筛选。 data() {…

vue实现心电图

vue实现心电图

Vue 实现心电图效果 在 Vue 中实现心电图效果通常需要结合 Canvas 或 SVG 进行动态绘图,模拟心电图数据的实时变化。以下是两种实现方式的核心思路和代码示例: 使用 Canvas 绘制…