当前位置:首页 > 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);

局部注册组件

仅在当前组件内可用:

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 数据传递

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

<!-- 父组件 -->
<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>

生命周期钩子

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

vue实现组件

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

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

相关文章

vue权限实现

vue权限实现

Vue 权限实现方案 基于路由的权限控制 通过路由守卫实现权限验证,在路由配置中添加meta字段标记权限要求。在router.beforeEach中检查用户权限与路由要求的匹配情况。 // rout…

vue实现开关

vue实现开关

Vue 实现开关组件的方法 使用原生HTML和Vue数据绑定 通过v-model绑定复选框的checked状态,结合CSS样式实现开关效果: <template> <label…

vue watch实现

vue watch实现

在 Vue 中实现 watch 监听数据变化 Vue 的 watch 选项用于监听数据变化并执行相应的回调函数。以下是几种常见的实现方式: 基础用法 export default { data(…

vue实现滚屏

vue实现滚屏

实现滚屏的基本方法 在Vue中实现滚屏效果可以通过多种方式完成,常见的有原生JavaScript滚动方法、第三方库或CSS动画。以下是几种常见实现方式: 使用window.scrollTo或Elem…

vue实现eventbus

vue实现eventbus

Vue 中实现 EventBus 在 Vue 中,EventBus 是一种跨组件通信的机制,尤其适用于非父子组件之间的数据传递。以下是实现 EventBus 的几种方法: 方法一:使用 Vue 实例…

vue 实现循环

vue 实现循环

Vue 实现循环的方法 在 Vue 中,可以使用 v-for 指令来实现循环渲染列表数据。以下是几种常见的循环实现方式: 基本数组循环 <template> <ul>…