当前位置:首页 > VUE

vue 组件实现

2026-03-07 05:49:41VUE

Vue 组件实现方法

Vue 组件是 Vue.js 的核心概念之一,用于封装可复用的代码。以下是 Vue 组件的几种实现方式。

单文件组件 (SFC)

单文件组件将模板、逻辑和样式封装在一个 .vue 文件中,结构清晰且易于维护。

<template>
  <div>
    <h1>{{ title }}</h1>
    <button @click="handleClick">Click Me</button>
  </div>
</template>

<script>
export default {
  name: 'MyComponent',
  data() {
    return {
      title: 'Hello Vue'
    }
  },
  methods: {
    handleClick() {
      alert('Button clicked!')
    }
  }
}
</script>

<style scoped>
h1 {
  color: #42b983;
}
</style>

全局组件注册

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

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

new Vue({
  el: '#app'
})

局部组件注册

在 Vue 实例的 components 选项中注册局部组件,仅在该实例作用域内可用。

const ChildComponent = {
  template: '<div>Child Component</div>'
}

new Vue({
  el: '#app',
  components: {
    'child-component': ChildComponent
  }
})

动态组件

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

<template>
  <div>
    <button @click="currentComponent = 'ComponentA'">Show A</button>
    <button @click="currentComponent = 'ComponentB'">Show B</button>
    <component :is="currentComponent"></component>
  </div>
</template>

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

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

函数式组件

函数式组件无状态、无实例,适用于纯渲染场景。

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

组件通信

父子组件通过 props$emit 实现通信。

<!-- Parent Component -->
<template>
  <child-component :message="parentMessage" @update="handleUpdate" />
</template>

<script>
export default {
  data() {
    return {
      parentMessage: 'Hello from parent'
    }
  },
  methods: {
    handleUpdate(newMessage) {
      this.parentMessage = newMessage
    }
  }
}
</script>

<!-- Child Component -->
<template>
  <div>
    <p>{{ message }}</p>
    <button @click="notifyParent">Notify Parent</button>
  </div>
</template>

<script>
export default {
  props: ['message'],
  methods: {
    notifyParent() {
      this.$emit('update', 'New message from child')
    }
  }
}
</script>

插槽 (Slots)

插槽用于内容分发,实现更灵活的组件结构。

<!-- Parent Component -->
<template>
  <child-component>
    <template v-slot:header>
      <h1>Header Content</h1>
    </template>
    <p>Default slot content</p>
  </child-component>
</template>

<!-- Child Component -->
<template>
  <div>
    <slot name="header"></slot>
    <slot></slot>
  </div>
</template>

混入 (Mixins)

混入用于复用组件选项逻辑。

vue 组件实现

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

export default {
  mixins: [myMixin]
}

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

相关文章

vue 实现tabs

vue 实现tabs

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

vue实现闪烁

vue实现闪烁

Vue实现元素闪烁效果 使用CSS动画实现 通过Vue绑定class结合CSS动画实现闪烁效果,代码简洁且性能较好。 <template> <div :class="{ 'bl…

vue树形实现

vue树形实现

Vue 树形组件实现方法 使用递归组件实现树形结构 递归组件是 Vue 中实现树形结构的常见方法。通过组件调用自身的方式,可以轻松构建多层嵌套的树形结构。 <template> &l…

vue实现mui

vue实现mui

使用 Vue 实现 MUI(Material-UI)风格的界面 Vue 本身不直接提供 MUI(Material Design)组件,但可以通过集成第三方库或手动实现来达到类似效果。以下是几种常见方法…

vue实现链接

vue实现链接

Vue 实现链接的方法 在 Vue 中实现链接可以通过多种方式,包括使用 <router-link>、<a> 标签或编程式导航。以下是几种常见的方法: 使用 <rout…

vue实现京东

vue实现京东

Vue 实现京东电商网站的关键步骤 项目搭建与基础配置 使用 Vue CLI 或 Vite 初始化项目,安装 Vue Router 管理路由,Vuex/Pinia 管理状态。配置基础 UI 框架如 E…