当前位置:首页 > VUE

vue实现组件开发创建

2026-01-23 02:02:28VUE

Vue 组件开发创建步骤

安装 Vue CLI
确保已安装 Node.js 和 npm,通过以下命令全局安装 Vue CLI:

npm install -g @vue/cli

创建 Vue 项目
使用 Vue CLI 创建新项目,选择默认配置或手动配置:

vue create my-project

项目结构
创建完成后,项目目录结构通常包含:

  • src/components:存放自定义组件
  • src/App.vue:根组件
  • src/main.js:入口文件

编写组件

单文件组件结构
src/components 下创建 .vue 文件,例如 MyComponent.vue,包含三部分:

vue实现组件开发创建

<template>
  <div>{{ message }}</div>
</template>

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

<style scoped>
div {
  color: blue;
}
</style>

注册组件
在父组件中引入并注册:

<template>
  <MyComponent />
</template>

<script>
import MyComponent from './components/MyComponent.vue';

export default {
  components: {
    MyComponent
  }
};
</script>

组件通信

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

<!-- 父组件 -->
<template>
  <ChildComponent :msg="parentMessage" />
</template>

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

export default {
  components: { ChildComponent },
  data() {
    return {
      parentMessage: 'Data from parent'
    };
  }
};
</script>

<!-- 子组件 ChildComponent.vue -->
<template>
  <div>{{ msg }}</div>
</template>

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

自定义事件通信
子组件通过 $emit 触发事件,父组件监听:

vue实现组件开发创建

<!-- 子组件 -->
<template>
  <button @click="notifyParent">Click Me</button>
</template>

<script>
export default {
  methods: {
    notifyParent() {
      this.$emit('child-event', 'Data from child');
    }
  }
};
</script>

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

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

export default {
  components: { ChildComponent },
  methods: {
    handleEvent(data) {
      console.log(data); // 输出 "Data from child"
    }
  }
};
</script>

生命周期钩子

常用生命周期钩子示例:

<script>
export default {
  created() {
    console.log('组件实例创建完成');
  },
  mounted() {
    console.log('DOM 挂载完成');
  },
  beforeUnmount() {
    console.log('组件卸载前');
  }
};
</script>

进阶用法

插槽(Slots)
父组件向子组件插入内容:

<!-- 子组件 -->
<template>
  <div>
    <slot name="header"></slot>
    <slot></slot>
  </div>
</template>

<!-- 父组件 -->
<template>
  <ChildComponent>
    <template v-slot:header>
      <h1>Header Content</h1>
    </template>
    <p>Default Content</p>
  </ChildComponent>
</template>

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

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

注意事项

  • 组件命名建议使用 PascalCase(如 MyComponent)或 kebab-case(如 my-component)。
  • 样式加 scoped 属性可避免全局污染。
  • 复杂状态管理推荐使用 Vuex 或 Pinia。

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

相关文章

vue表格实现教学

vue表格实现教学

Vue 表格实现方法 使用原生HTML表格 在Vue模板中直接使用HTML的<table>标签,结合v-for动态渲染数据: <template> <table&g…

vue实现选区

vue实现选区

Vue 实现选区的基本方法 在Vue中实现选区功能通常涉及DOM操作和事件处理。以下是几种常见的方法: 使用原生JavaScript的Selection API 通过window.getSelec…

vue实现sso

vue实现sso

Vue 实现 SSO(单点登录)方案 单点登录(SSO)允许用户通过一次登录访问多个相关系统。以下是基于 Vue 的实现方案: 使用 OAuth2/OpenID Connect 协议 OAuth2…

vue实现滑块

vue实现滑块

Vue 实现滑块组件的方法 使用原生 HTML 和 Vue 指令 通过 Vue 的 v-model 和事件监听实现基础滑块功能。创建一个包含 input 元素的组件,类型设置为 range,并绑定到…

vue实现剪辑

vue实现剪辑

Vue 实现视频剪辑功能 在Vue中实现视频剪辑功能通常需要结合HTML5的<video>元素和第三方库(如FFmpeg.js或Video.js)。以下是几种常见的实现方法: 使用FFm…

computed vue 实现

computed vue 实现

computed 在 Vue 中的基本用法 Vue 的 computed 属性用于声明依赖于其他数据的计算属性。当依赖的数据变化时,计算属性会自动重新计算并缓存结果。 export defaul…