当前位置:首页 > VUE

vue3实现

2026-03-09 19:29:11VUE

Vue 3 实现方法

Vue 3 提供了多种方式来实现功能,包括组合式 API、选项式 API 以及基于 <script setup> 的语法糖。以下是常见的实现方法:

组合式 API

组合式 API 是 Vue 3 的核心特性之一,允许更灵活地组织逻辑代码。

import { ref, computed, onMounted } from 'vue';

export default {
  setup() {
    const count = ref(0);
    const doubleCount = computed(() => count.value * 2);

    function increment() {
      count.value++;
    }

    onMounted(() => {
      console.log('Component mounted');
    });

    return {
      count,
      doubleCount,
      increment,
    };
  },
};

<script setup> 语法糖

<script setup> 是 Vue 3 的语法糖,简化了组合式 API 的使用。

<script setup>
import { ref, computed } from 'vue';

const count = ref(0);
const doubleCount = computed(() => count.value * 2);

function increment() {
  count.value++;
}
</script>

<template>
  <button @click="increment">{{ count }} (Double: {{ doubleCount }})</button>
</template>

选项式 API

Vue 3 仍然支持 Vue 2 的选项式 API,适合简单场景。

export default {
  data() {
    return {
      count: 0,
    };
  },
  computed: {
    doubleCount() {
      return this.count * 2;
    },
  },
  methods: {
    increment() {
      this.count++;
    },
  },
  mounted() {
    console.log('Component mounted');
  },
};

响应式数据管理

Vue 3 的响应式系统基于 refreactive

使用 ref

ref 用于基本类型数据。

import { ref } from 'vue';

const num = ref(0);
console.log(num.value); // 访问值

使用 reactive

reactive 用于对象类型数据。

import { reactive } from 'vue';

const state = reactive({
  count: 0,
  name: 'Vue 3',
});
console.log(state.count); // 直接访问属性

生命周期钩子

Vue 3 的生命周期钩子可以通过 onMountedonUpdated 等函数调用。

import { onMounted, onUpdated } from 'vue';

onMounted(() => {
  console.log('Component mounted');
});

onUpdated(() => {
  console.log('Component updated');
});

组件通信

Vue 3 支持多种组件通信方式。

Props 和 Emits

父组件通过 props 传递数据,子组件通过 emits 触发事件。

// 子组件
<script setup>
const props = defineProps({
  message: String,
});
const emit = defineEmits(['update']);

function handleClick() {
  emit('update', 'New message');
}
</script>

Provide / Inject

跨层级组件通信。

// 祖先组件
import { provide } from 'vue';
provide('key', 'value');

// 后代组件
import { inject } from 'vue';
const value = inject('key');

状态管理

对于复杂状态管理,可以使用 Pinia 或 Vuex。

Pinia 示例

Pinia 是 Vue 3 推荐的状态管理库。

// store.js
import { defineStore } from 'pinia';

export const useCounterStore = defineStore('counter', {
  state: () => ({ count: 0 }),
  actions: {
    increment() {
      this.count++;
    },
  },
});

// 组件中使用
import { useCounterStore } from './store';
const counter = useCounterStore();
counter.increment();

路由管理

Vue Router 4 是 Vue 3 的官方路由库。

vue3实现

import { createRouter, createWebHistory } from 'vue-router';

const router = createRouter({
  history: createWebHistory(),
  routes: [
    { path: '/', component: Home },
    { path: '/about', component: About },
  ],
});

总结

Vue 3 提供了组合式 API、<script setup> 语法糖等现代化特性,同时兼容选项式 API。响应式系统、组件通信、状态管理和路由管理均有优化,适合构建复杂应用。

分享给朋友:

相关文章

vue实现a4打印模板

vue实现a4打印模板

使用Vue实现A4打印模板 在Vue中实现A4打印模板需要结合CSS的打印样式和Vue的模板渲染能力。以下是具体实现方法: 设置A4纸张尺寸 在CSS中定义A4纸张的标准尺寸(210mm × 297…

vue实现通知

vue实现通知

Vue 实现通知功能的方法 使用 Vue 实现通知功能可以通过多种方式实现,以下是一些常见的方法: 使用 Vue 的全局事件总线 在 Vue 中可以通过事件总线实现通知功能。创建一个全局事件总线,然…

vue实现pie

vue实现pie

Vue 实现饼图 在 Vue 中实现饼图通常可以通过第三方图表库(如 ECharts、Chart.js 或 Highcharts)来完成。以下是使用 ECharts 和 Chart.js 的两种常见方…

vue实现返回

vue实现返回

返回按钮功能实现 在Vue中实现返回功能通常有两种方式:使用浏览器历史记录的history.back()或结合Vue Router进行编程式导航。以下是具体实现方法: 使用浏览器API metho…

vue实现长按

vue实现长按

Vue 实现长按功能的方法 在 Vue 中实现长按功能可以通过原生事件监听或自定义指令完成。以下是几种常见实现方式: 使用原生事件监听 通过 @mousedown 和 @mouseup 或 @tou…

vue滚动实现

vue滚动实现

Vue 滚动实现方法 使用原生滚动 在 Vue 中可以通过 ref 获取 DOM 元素,调用原生滚动方法实现滚动效果。适用于简单场景。 <template> <div ref=…