当前位置:首页 > VUE

实现vue组件

2026-02-09 23:24:53VUE

创建 Vue 组件

Vue 组件是 Vue.js 应用的核心构建块,可以封装可复用的功能。以下是创建 Vue 组件的几种常见方式。

单文件组件 (SFC) 单文件组件是最常用的方式,将模板、逻辑和样式封装在一个 .vue 文件中。

<template>
  <div>
    <h1>{{ title }}</h1>
    <button @click="increment">Click me</button>
    <p>Count: {{ count }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      title: 'My Component',
      count: 0
    }
  },
  methods: {
    increment() {
      this.count++
    }
  }
}
</script>

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

全局注册组件 在 Vue 应用中全局注册组件,使其在任何地方可用。

import MyComponent from './MyComponent.vue'

app.component('MyComponent', MyComponent)

局部注册组件 仅在特定组件中注册,避免全局污染。

实现vue组件

import MyComponent from './MyComponent.vue'

export default {
  components: {
    MyComponent
  }
}

组件通信

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

<!-- ParentComponent.vue -->
<template>
  <ChildComponent :message="parentMessage" />
</template>

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

export default {
  components: { ChildComponent },
  data() {
    return {
      parentMessage: 'Hello from parent'
    }
  }
}
</script>
<!-- ChildComponent.vue -->
<template>
  <p>{{ message }}</p>
</template>

<script>
export default {
  props: ['message']
}
</script>

自定义事件 子组件通过 $emit 向父组件发送事件。

实现vue组件

<!-- ChildComponent.vue -->
<template>
  <button @click="notifyParent">Notify Parent</button>
</template>

<script>
export default {
  methods: {
    notifyParent() {
      this.$emit('notify', 'Child clicked')
    }
  }
}
</script>
<!-- ParentComponent.vue -->
<template>
  <ChildComponent @notify="handleNotification" />
</template>

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

export default {
  components: { ChildComponent },
  methods: {
    handleNotification(message) {
      console.log(message)
    }
  }
}
</script>

生命周期钩子

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

export default {
  created() {
    console.log('Component created')
  },
  mounted() {
    console.log('Component mounted to DOM')
  },
  updated() {
    console.log('Component updated')
  },
  destroyed() {
    console.log('Component destroyed')
  }
}

动态组件

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

<template>
  <component :is="currentComponent" />
  <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>

插槽 (Slots)

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

<!-- ChildComponent.vue -->
<template>
  <div>
    <slot name="header"></slot>
    <slot></slot>
    <slot name="footer"></slot>
  </div>
</template>
<!-- ParentComponent.vue -->
<template>
  <ChildComponent>
    <template v-slot:header>
      <h1>Header Content</h1>
    </template>
    <p>Default slot content</p>
    <template v-slot:footer>
      <p>Footer Content</p>
    </template>
  </ChildComponent>
</template>

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

相关文章

vue设计与实现下载

vue设计与实现下载

vue设计与实现电子书下载 《Vue.js设计与实现》是一本深入解析Vue.js框架原理的书籍,由霍春阳(HcySunYang)撰写。以下是获取该资源的常见途径: 正版购买渠道 京东、…

vue实现密码框的包

vue实现密码框的包

Vue 密码框实现方案 在 Vue 中实现密码框功能,可以通过原生 HTML 结合 Vue 指令或使用第三方组件库完成。以下是几种常见方案: 原生 HTML + Vue 双向绑定 <t…

vue 动画 实现

vue 动画 实现

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

实现.vue文件

实现.vue文件

创建.vue文件的基本结构 一个标准的.vue文件包含三个部分:模板(template)、脚本(script)和样式(style)。这种结构允许开发者在一个文件中管理组件的HTML、JavaScrip…

vue实现图片

vue实现图片

Vue 实现图片功能的方法 静态图片引入 在 Vue 项目中,可以通过 src 属性直接引入静态图片。图片通常放在 public 或 assets 目录下。 <template> &…

vue el 实现

vue el 实现

Vue 中使用 Element UI (el) 的实现方法 Element UI 是一个基于 Vue 的组件库,提供丰富的 UI 组件,常用于快速构建企业级中后台产品。以下介绍 Vue 项目中集成和使…