<template>
<div>
<button @click="changeCounter">确认</button>
</div>
</template>
<script setup>
import { ref, watch } from 'vue'
const counter = ref(0)
//counter.value++
const changeCounter = () => {
counter.value++
}
watch(counter, (newValue, oldValue) => {
console.log('The new counter value is: ' + counter.value)
console.log("new counter======{}==",newValue);
console.log("old counter======{}==",oldValue);
})
return {
counter,
changeCounter
}
</script>
错误
<template>
<div>
</div>
</template>
<script setup>
import { ref, watch } from 'vue'
const counter = ref(0)
counter.value = '5'
watch(counter, (newValue, oldValue) => {
console.log('The new counter value is: ' + counter.value)
})
</script>
这个为啥不行?
<template>
<div id="app">
<p>计数器:{{ counter }}</p>
<el-button type="primary" @click="counter++">
Click
</el-button>
</div>
</template>
<script setup>
import { ref, watch } from 'vue'
const counter = ref(0)
watch(counter, (newValue, oldValue) => {
console.log('The new counter value is: ' + counter.value)
})
</script>