Composition vs Options

对比

Composition

通过 import 一系列 API 来定义组件。

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

// reactive state
const count = ref(0)

// functions that mutate state and trigger updates
function increment() {
  count.value++
}

// lifecycle hooks
onMounted(() => {
  console.log(`The initial count is ${count.value}.`)
})
</script>

<template>
  <button @click="increment">Count is: {{ count }}</button>
</template>

Options

将组件的逻辑写在一个对象中。其中的 this 指向组件实例。

如何选择?

// TODO

https://vuejs.org/guide/extras/composition-api-faq.html

Last updated