执行setup函数时可以访问:props、attrs、slots、emit,不可以访问:data、computed、methodsimport { ref, reactive, toRefs, isRef } from 'vue'
export defalut {
props:{
title: String
},
setup( props, context ){
console.log(props.title);
const { title } = toRefs(props)
let count = ref(0)
console.log(count.valeu);
console.log(isRef(count));
let arr = reactive([1,3,5,7,9])
let addOne = () => {
count.value++
}
console.log(context.attrs)
console.log(context.slots)
console.log(content.emit)
return {
count, arr, addOne
}
}
}
使用setup语法糖
<template>
<div>
<p>
<span @click="update(count-1)">-</span>
{{ count }}
<span @click="update(count+1)">+</span>
</p>
</div>
</template>
<script lang="ts" setup name="component-name">
import { ref, reactive, toRefs, isRef } from 'vue'
let count = ref(0)
function update(value) {
count.value = value
}
</script>