V3-setup 语法糖-reactive相关

52 阅读1分钟

Reactive 相关

reactive和ref相似,通过proxy代理成响应数据 不同; ref支持代理任何数据类型, reactive 仅支持引用数据类型,泛型, Map Set

// setup 
import { reactive, ref, readonly } from 'vue'
// ref  reactive 的区别
// ref 支持所有数据类型
// reavtive // 引用数据类型,泛型  Map Set
const form = reactive<string[]>([])
const clickBtn = () => {
  // form = ['ssss', 'bbbbb']   // = 赋值破坏reactive 响应结构,改变了栈指向,可以通过push  + [...arr] 处理
  form.push('666666') // 操作数组不影响引用数据类型栈指向
}

// template 部分
<template lang="">
  <div v-for="item in form">
    {{item}}
  </div>
  <button @click= "clickBtn">点醒你</button>
</template>

Tip: shallowReactive 和 shallowRef 形似也是浅层响应,和 reactive 同时触发时,变成了深层响应(不建议一起使用)

// readonly 响应式只读属性锁定
const obj = {
  total: 888
}
const read = readonly(obj)