链接上一篇文章:vue3学习之ref的妙用
reactive是把一系列响应式数据放进去的一个函数
toRefs是将变量转化为一个ref对象让它能够在视图中进行数据绑定
<template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png">
<h1>{{count}}</h1>
<h1>{{double}}</h1>
<button @click="increase">赞+1</button><br/>
</div>
</template>
<script lang="ts">
// 响应式对象引入,计算函数引入
import { ref,computed,reactive,toRefs } from 'vue'
interface DataProps {
count: number;
double: number;
increase: () => void;
}
export default{
name: 'App',
setup() {
const data: DataProps = reactive({
count: 0,
increase: () => { data.count++},
double: computed(() => data.count * 2),
})
const refData = toRefs(data)
return {
...refData
}
}
};
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
效果和上一篇显示的是一样的。