vue3 之 组合式API—reactive和ref函数

ref()

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

核心步骤:
1️⃣ 从 vue 包中导入 ref 函数
2️⃣在

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

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

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

核心步骤:
1️⃣ 从 vue 包中导入 reactive 函数
2️⃣ 在

在这里插入图片描述



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

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

1️⃣ reactive和ref函数的共同作用是什么 ?
用函数调用的方式生成响应式数据

2️⃣ reactive vs ref ?
reactive不能处理简单类型的数据
ref参数类型支持更好但是必须通过.value访问修改
ref函数的内部实现依赖于reactive函数

3️⃣ 在实际工作中推荐使用哪个?
推荐使用ref函数,更加灵活

相关推荐

  1. Vue3 组合 API: reactive ref 函数

    2024-02-04 17:18:03       21 阅读
  2. vue3组合函数

    2024-02-04 17:18:03       21 阅读
  3. vue3-响应基础ref

    2024-02-04 17:18:03       33 阅读
  4. Vue3ref函数reactive函数setup函数

    2024-02-04 17:18:03       39 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-04 17:18:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-04 17:18:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-04 17:18:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-04 17:18:03       18 阅读

热门阅读

  1. IdleHandler的简单认识

    2024-02-04 17:18:03       34 阅读
  2. 学习数据结构的第一天

    2024-02-04 17:18:03       31 阅读
  3. CSS基础

    CSS基础

    2024-02-04 17:18:03      24 阅读
  4. linux编译ffmpeg动态库

    2024-02-04 17:18:03       31 阅读
  5. 如何有效的开展接口自动化测试(超详细整理)

    2024-02-04 17:18:03       33 阅读
  6. Spring依赖注入原理与最佳实践

    2024-02-04 17:18:03       37 阅读
  7. Pinia:一个Vue的状态管理库

    2024-02-04 17:18:03       30 阅读
  8. 面试高频知识点:2线程 2.1.4 线程池常用参数

    2024-02-04 17:18:03       31 阅读