专业的编程技术博客社区

网站首页 > 博客文章 正文

vue3中使用ref函数代替响应式data

baijin 2025-06-08 19:08:26 博客文章 1 ℃ 0 评论

<script setup>

import { ref } from 'vue';

const state = ref({

count:0

})

const setState = ()=>{

state.value.count++

}

const count = ref(0)

const setCount = ()=>{

count.value++

}

</script>

<template>

<div>


<button @click="setState"> {{ state.count }} </button>

<button @click="setCount"> {{ count }} </button>

</div>

</template>

使用ref的时候必须使用.value.

如果reactive函数,则该函数只支持传递对象。

ref则两个都能传递。

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表