vue3中reactive和ref函数及对比

 reactive和ref函数

1. reactive

接受对象类型数据的参数传入并返回一个响应式的对象

<script setup>
 // 导入
 import { reactive } from 'vue'
 // 执行函数 传入参数 变量接收
 const state = reactive({
   msg:'this is msg'
 })
 const setSate = ()=>{
   // 修改数据更新视图
   state.msg = 'this is new msg'//不需要.value
 }
</script>

<template>
  {
  { state.msg }}
  <button @click="setState">change msg</button>
</template>

2. ref

接收简单类型或者对象类型的数据传入并返回一个响应式的对象

<script setup>
 // 导入
 import { ref } from 'vue'
 // 执行函数 传入参数 变量接收
 const count = ref(0)
 const setCount = ()=>{
   // 修改数据更新视图必须加上.value
   count.value++
 }
</script>

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

注意:

  • ref函数创建响应式数据,返回值是一个对象

  • 模版中使用ref数据,省略.value,js代码中不能省略(特殊:js中watch监听可以省)

3、reactive 对比 ref

  1. 都是用来生成响应式数据

  2. 不同点:

    1. reactive不能处理简单类型的数据,只支持引用数据类型,ref支持基本和引用数据类型

    2. ref通过.value获取数据,reactive不需要.value

    3. ref创建响应式引用数据类型低层依赖reactive

 

相关推荐

  1. vue3reactiveref函数对比

    2024-01-19 07:30:03       39 阅读
  2. vue3reactiveref函数对比

    2024-01-19 07:30:03       24 阅读
  3. vue3refreactive对比

    2024-01-19 07:30:03       18 阅读
  4. VUE3——reactive对比ref

    2024-01-19 07:30:03       18 阅读
  5. vue3reactiveref

    2024-01-19 07:30:03       8 阅读
  6. vue3reactiveref

    2024-01-19 07:30:03       9 阅读
  7. Vue3ref函数reactive函数setup函数

    2024-01-19 07:30:03       40 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-19 07:30:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-19 07:30:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-19 07:30:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-19 07:30:03       20 阅读

热门阅读

  1. 集中常见的排序方法Go语言版本实现

    2024-01-19 07:30:03       36 阅读
  2. 使用Spring管理Caffeine缓存(CacheManager+Caffeine)

    2024-01-19 07:30:03       37 阅读
  3. 家庭家用服务全方面机器人

    2024-01-19 07:30:03       39 阅读
  4. axios的使用以及Vue动画

    2024-01-19 07:30:03       35 阅读
  5. axios原理

    2024-01-19 07:30:03       32 阅读
  6. Dynamo 使用小结

    2024-01-19 07:30:03       30 阅读
  7. spark+phoenix读取hbase

    2024-01-19 07:30:03       35 阅读