当前位置:首页 > VUE

vue3实现写法

2026-01-20 09:16:12VUE

Vue 3 的基本实现写法

Vue 3 引入了 Composition API,提供了更灵活的代码组织方式。以下是 Vue 3 的基本实现写法示例。

选项式 API(Options API)

选项式 API 是 Vue 2 的延续,适合熟悉 Vue 2 的开发者。

<template>
  <div>
    <p>{{ message }}</p>
    <button @click="increment">Increment</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      message: 'Hello Vue 3!',
      count: 0
    }
  },
  methods: {
    increment() {
      this.count++
    }
  }
}
</script>

组合式 API(Composition API)

组合式 API 是 Vue 3 的新特性,更适合逻辑复用和复杂组件。

<template>
  <div>
    <p>{{ message }}</p>
    <button @click="increment">Increment</button>
  </div>
</template>

<script>
import { ref } from 'vue'

export default {
  setup() {
    const message = ref('Hello Vue 3!')
    const count = ref(0)

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

    return {
      message,
      count,
      increment
    }
  }
}
</script>

使用 <script setup> 语法糖

<script setup> 是组合式 API 的语法糖,简化了代码。

<template>
  <div>
    <p>{{ message }}</p>
    <button @click="increment">Increment</button>
  </div>
</template>

<script setup>
import { ref } from 'vue'

const message = ref('Hello Vue 3!')
const count = ref(0)

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

Vue 3 的生命周期钩子

Vue 3 的生命周期钩子可以通过组合式 API 使用。

<script setup>
import { onMounted, onUpdated, onUnmounted } from 'vue'

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

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

onUnmounted(() => {
  console.log('Component unmounted')
})
</script>

Vue 3 的响应式数据

Vue 3 使用 refreactive 来创建响应式数据。

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

const count = ref(0)
const state = reactive({
  name: 'Vue 3',
  version: '3.2.0'
})
</script>

Vue 3 的计算属性

使用 computed 创建计算属性。

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

const count = ref(0)
const doubleCount = computed(() => count.value * 2)
</script>

Vue 3 的侦听器

使用 watchwatchEffect 创建侦听器。

<script setup>
import { ref, watch, watchEffect } from 'vue'

const count = ref(0)

watch(count, (newValue, oldValue) => {
  console.log(`Count changed from ${oldValue} to ${newValue}`)
})

watchEffect(() => {
  console.log(`Count is now ${count.value}`)
})
</script>

Vue 3 的组件通信

Vue 3 提供了 propsemit 进行组件通信。

vue3实现写法

父组件:

<template>
  <ChildComponent :message="parentMessage" @update="handleUpdate" />
</template>

<script setup>
import { ref } from 'vue'
import ChildComponent from './ChildComponent.vue'

const parentMessage = ref('Hello from parent')

const handleUpdate = (newMessage) => {
  parentMessage.value = newMessage
}
</script>

子组件:

<template>
  <div>
    <p>{{ message }}</p>
    <button @click="updateMessage">Update</button>
  </div>
</template>

<script setup>
const props = defineProps({
  message: String
})

const emit = defineEmits(['update'])

const updateMessage = () => {
  emit('update', 'Updated message from child')
}
</script>

Vue 3 的路由

使用 Vue Router 4 进行路由管理。

安装 Vue Router:

npm install vue-router@4

路由配置:

vue3实现写法

import { createRouter, createWebHistory } from 'vue-router'
import Home from './views/Home.vue'
import About from './views/About.vue'

const routes = [
  { path: '/', component: Home },
  { path: '/about', component: About }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

export default router

在组件中使用路由:

<script setup>
import { useRouter, useRoute } from 'vue-router'

const router = useRouter()
const route = useRoute()

const goToAbout = () => {
  router.push('/about')
}
</script>

Vue 3 的状态管理

使用 Pinia 进行状态管理。

安装 Pinia:

npm install pinia

创建 store:

import { defineStore } from 'pinia'

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

在组件中使用 store:

<script setup>
import { useCounterStore } from './stores/counter'

const counter = useCounterStore()
</script>

<template>
  <div>
    <p>{{ counter.count }}</p>
    <button @click="counter.increment">Increment</button>
  </div>
</template>

以上是 Vue 3 的基本实现写法,涵盖了选项式 API、组合式 API、生命周期、响应式数据、计算属性、侦听器、组件通信、路由和状态管理等内容。

标签: 写法
分享给朋友:

相关文章

jquery写法

jquery写法

jQuery 基础语法与常用方法 jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。以下是 jQuery 的基本写法和常用操作。…

uniapp组件写法

uniapp组件写法

uniapp组件的基本结构 在uniapp中创建组件通常需要遵循Vue单文件组件(SFC)规范。一个典型的组件文件包含<template>、<script>和<style…

vue3实现写法

vue3实现写法

以下是 Vue 3 的核心实现写法示例,涵盖组合式 API、响应式数据、组件通信等常见场景: 组合式 API 基础写法 <script setup> import { ref, rea…