当前位置:首页 > VUE

vue实现组件

2026-01-08 03:23:52VUE

Vue 组件实现基础

Vue 组件是可复用的 Vue 实例,用于封装 UI 和逻辑。通过 .vue 文件或直接注册组件实现。

单文件组件 (SFC) 示例

<template>
  <div class="example-component">
    <p>{{ message }}</p>
    <button @click="changeMessage">点击修改</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      message: "Hello Vue!"
    };
  },
  methods: {
    changeMessage() {
      this.message = "Message changed!";
    }
  }
};
</script>

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

全局注册组件

main.js 或入口文件中全局注册,所有地方可直接使用:

import ExampleComponent from './components/ExampleComponent.vue';
Vue.component('example-component', ExampleComponent);

局部注册组件

仅在当前组件内可用:

vue实现组件

import ExampleComponent from './components/ExampleComponent.vue';

export default {
  components: {
    'example-component': ExampleComponent
  }
};

动态组件

通过 <component :is> 动态切换组件:

<template>
  <component :is="currentComponent"></component>
  <button @click="toggleComponent">切换组件</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>

Props 数据传递

父组件向子组件传递数据:

vue实现组件

<!-- 父组件 -->
<template>
  <child-component :title="parentTitle"></child-component>
</template>

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

export default {
  components: { ChildComponent },
  data() {
    return {
      parentTitle: "来自父组件的标题"
    };
  }
};
</script>

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

<script>
export default {
  props: ['title'] // 或使用对象形式定义类型/默认值
};
</script>

事件通信(子 → 父)

子组件通过 $emit 触发父组件事件:

<!-- 子组件 -->
<template>
  <button @click="notifyParent">通知父组件</button>
</template>

<script>
export default {
  methods: {
    notifyParent() {
      this.$emit('custom-event', '子组件数据');
    }
  }
};
</script>

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

<script>
export default {
  methods: {
    handleEvent(data) {
      console.log(data); // 输出 "子组件数据"
    }
  }
};
</script>

插槽(Slots)

分发内容到子组件:

<!-- 父组件 -->
<template>
  <child-component>
    <p>插入到子组件的插槽内容</p>
  </child-component>
</template>

<!-- 子组件 -->
<template>
  <div>
    <slot></slot> <!-- 插槽内容渲染位置 -->
  </div>
</template>

生命周期钩子

在组件不同阶段执行逻辑:

export default {
  created() {
    console.log('组件实例已创建');
  },
  mounted() {
    console.log('DOM 挂载完成');
  },
  beforeDestroy() {
    console.log('组件销毁前');
  }
};

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

相关文章

vue 实现多选

vue 实现多选

Vue 实现多选的方法 在 Vue 中实现多选功能可以通过多种方式完成,以下是几种常见的实现方法: 使用 v-model 绑定数组 Vue 的 v-model 指令可以与复选框结合使用,绑定到一个…

vue实现tab标签

vue实现tab标签

Vue 实现 Tab 标签的方法 使用动态组件和 v-for 渲染标签 通过 v-for 循环生成标签页标题,结合 v-bind:class 动态切换激活状态。使用 v-show 或动态组件 <…

vue实现共享屏幕

vue实现共享屏幕

共享屏幕的基本实现 在Vue中实现共享屏幕功能通常需要结合浏览器的getDisplayMedia API。以下是一个基础实现示例: <template> <div>…

vue实现年历

vue实现年历

Vue 实现年历的方法 使用 Vue 实现年历可以通过组件化开发方式,结合日期计算和动态渲染完成。以下是两种常见的实现方式: 基于原生 Vue 的日历组件 创建 Calendar.vue 组件文件,…

vue卖座网实现

vue卖座网实现

Vue 卖座网实现 项目结构搭建 使用 Vue CLI 快速初始化项目,安装必要依赖如 Vue Router、Vuex、Axios。 创建核心目录结构:components(通用组件)、views(页…

vue如何实现到期提醒

vue如何实现到期提醒

实现 Vue 到期提醒功能 使用计算属性计算剩余时间 在 Vue 组件中定义一个计算属性,计算目标日期与当前日期的差值。通过 Date 对象获取时间戳并计算剩余天数或小时数。 computed:…