基本用法
const App = Vue.createApp({
setup(){
const {ref,computed} = Vue;
const count = ref(0);
const handleClick = ()=>{
count.value += 1
}
const countAddFive = computed(()=>{
return count.value + 5
})
return {
count,
handleClick
}
},
template:`
<div>
<span @click="handleClick">{{count}}</span> ---{{countAddFive}}
</div>
`
})
复杂用法
const App = Vue.createApp({
setup(){
const {ref,computed} = Vue;
const count = ref(0);
const handleClick = ()=>{
count.value + 1
}
const handAddFive = computed({
get:()=>{
return count.value + 1;
},
set:(params)=>{
count.value = params + 10
}
})
setTimeout(()=>{
countAddFive.value = 100
},3000)
return {
count,
handleClick,
handAddFive
}
},
template:`
<div>
<span @click="handleClick">{{count}}</span> ---{{countAddFive}}
</div>
`
})