reactive

reactive: 更改可以触发更新

Options

declare reactive state (using the data component option):

1
2
3
4
5
6
7
export default {
data() {
return {
message: 'Hello World!'
}
}
}

The message property will be made available(可获得的) in the template:

1
2
3
4
<h1>{{ message }}</h1>

<!-- we can use any valid JavaScript expression: -->
<h1>{{ message.split('').reverse().join('') }}</h1>

Composition

<script setup>

declare reactive state using Vue’s reactive() API:

1
2
3
4
5
6
7
8
import { reactive } from 'vue'

const counter = reactive({
count: 0
})

console.log(counter.count) // 0
counter.count++

reactive() 只适用于对象(包括数组和 MapSet 等内置类型)

ref() 可以采用任何值类型,并创建一个对象 that exposes the inner value under a .value property:

1
2
3
4
5
6
import { ref } from 'vue'

const message = ref('Hello World!')

console.log(message.value) // "Hello World!"
message.value = 'Changed'

更多关于 reactive() and ref()https://vuejs.org/guide/essentials/reactivity-fundamentals.html

template