当前位置:首页 > VUE

js实现vue组件

2026-02-20 14:28:49VUE

Vue 组件的基本实现

在 JavaScript 中实现 Vue 组件可以通过 Vue.js 的选项式 API 或组合式 API 来完成。以下分别展示两种方式的实现方法。

选项式 API 实现

// 定义一个 Vue 组件
const MyComponent = {
  template: `
    <div>
      <h1>{{ title }}</h1>
      <button @click="increment">点击计数: {{ count }}</button>
    </div>
  `,
  data() {
    return {
      title: 'Vue 组件示例',
      count: 0
    }
  },
  methods: {
    increment() {
      this.count++
    }
  }
}

// 注册组件
Vue.component('my-component', MyComponent)

// 创建 Vue 实例
new Vue({
  el: '#app'
})

组合式 API 实现 (Vue 3)

// 使用组合式 API 定义组件
const { createApp, ref } = Vue

const MyComponent = {
  template: `
    <div>
      <h1>{{ title }}</h1>
      <button @click="increment">点击计数: {{ count }}</button>
    </div>
  `,
  setup() {
    const title = 'Vue 组件示例'
    const count = ref(0)

    const increment = () => {
      count.value++
    }

    return {
      title,
      count,
      increment
    }
  }
}

// 创建应用并挂载
const app = createApp({
  components: {
    MyComponent
  }
})

app.mount('#app')

单文件组件 (SFC) 实现

在实际项目中,通常会使用单文件组件 (.vue 文件) 的方式组织代码:

<template>
  <div>
    <h1>{{ title }}</h1>
    <button @click="increment">点击计数: {{ count }}</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      title: 'Vue 组件示例',
      count: 0
    }
  },
  methods: {
    increment() {
      this.count++
    }
  }
}
</script>

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

组件通信方式

父组件向子组件传递数据 (Props)

// 子组件
const ChildComponent = {
  props: ['message'],
  template: '<div>接收的消息: {{ message }}</div>'
}

// 父组件
const ParentComponent = {
  components: { ChildComponent },
  template: '<child-component message="来自父组件的消息"></child-component>'
}

子组件向父组件发送事件 (Emit)

// 子组件
const ChildComponent = {
  template: `
    <button @click="sendMessage">发送消息</button>
  `,
  methods: {
    sendMessage() {
      this.$emit('message-sent', '来自子组件的消息')
    }
  }
}

// 父组件
const ParentComponent = {
  components: { ChildComponent },
  template: `
    <child-component @message-sent="handleMessage"></child-component>
  `,
  methods: {
    handleMessage(msg) {
      console.log('收到消息:', msg)
    }
  }
}

生命周期钩子

Vue 组件提供了一系列生命周期钩子,可以在不同阶段执行自定义逻辑:

const LifecycleDemo = {
  template: '<div>生命周期示例</div>',
  beforeCreate() {
    console.log('beforeCreate')
  },
  created() {
    console.log('created')
  },
  beforeMount() {
    console.log('beforeMount')
  },
  mounted() {
    console.log('mounted')
  },
  beforeUpdate() {
    console.log('beforeUpdate')
  },
  updated() {
    console.log('updated')
  },
  beforeDestroy() {
    console.log('beforeDestroy')
  },
  destroyed() {
    console.log('destroyed')
  }
}

动态组件

Vue 支持动态切换不同组件:

const ComponentA = { template: '<div>组件A</div>' }
const ComponentB = { template: '<div>组件B</div>' }

const App = {
  components: { ComponentA, ComponentB },
  data() {
    return {
      currentComponent: 'ComponentA'
    }
  },
  template: `
    <div>
      <button @click="currentComponent = 'ComponentA'">显示A</button>
      <button @click="currentComponent = 'ComponentB'">显示B</button>
      <component :is="currentComponent"></component>
    </div>
  `
}

以上示例展示了 Vue 组件的基本实现方式,包括组件定义、通信、生命周期和动态组件等核心功能。根据项目需求,可以选择选项式 API 或组合式 API 进行开发。

js实现vue组件

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

相关文章

js实现验证码

js实现验证码

使用Canvas生成图形验证码 在HTML中创建一个Canvas元素用于绘制验证码。通过JavaScript随机生成数字或字母组合,并添加干扰线、噪点等干扰元素增强安全性。 <canvas i…

js实现继承

js实现继承

原型链继承 通过将子类的原型指向父类的实例实现继承。子类实例可以访问父类原型上的属性和方法。 function Parent() { this.name = 'parent'; } Parent…

js实现文件下载

js实现文件下载

使用 a 标签下载文件 通过动态创建 a 标签并设置 download 属性实现文件下载。适用于已知文件 URL 或 Blob 数据的情况。 function downloadFile(url,…

js 实现继承

js 实现继承

原型链继承 通过让子类的原型对象指向父类的实例来实现继承。子类实例可以访问父类原型上的属性和方法。 function Parent() { this.name = 'parent'; } Pa…

js 实现vue

js 实现vue

Vue.js 的基本实现 在 JavaScript 中实现 Vue.js 的核心功能,可以通过数据绑定、响应式系统和虚拟 DOM 来实现。以下是实现 Vue.js 核心功能的简化版本。 数据响应式系…

js实现列表

js实现列表

使用 JavaScript 实现列表 JavaScript 提供了多种方式来实现列表功能,包括数组操作、DOM 元素动态生成等。以下是几种常见的实现方法: 使用数组存储列表数据 数组是 JavaSc…