别再死记硬背IIC时序图了!用Arduino UNO和逻辑分析仪,5分钟带你亲手抓取波形搞懂它
2026/5/9 21:44:30
在 Vue3 里,理解状态(State)和模型(Model)的区别,是选用 ref 或 reactive 的核心。
示例(ToB 项目中常见):
constloading=ref(false)// 是否加载中constcurrentRow=ref<Row|null>(null)// 当前选中行constisModalOpen=ref(false)// 模态框是否打开特点:
示例(表单对象):
constform=reactive({name:'',age:0,role:'',enable:true})特点:
| 场景 | 适合类型 | 使用原因 | 优缺点 |
|---|---|---|---|
| 状态 | 选中项、loading、modal开关、timer | 关注“变量是谁”,可能整体替换 | ✅ 可空、可替换、生命周期短 ❌ 访问深层字段需要 .value |
| 模型 | 表单对象、业务实体、配置对象 | 关注内部字段修改,不会整体替换 | ✅ 模板 v-model 友好、字段可直接访问 ❌ 整体替换不行、解构易失效 |
口诀:
关心整个变量是谁 → 状态 → ref
关心内部字段 → 模型 → reactive
constloading=ref(false)constcurrentRow=ref<Row|null>(null)constisModalOpen=ref(false)constselectRow=(row:Row)=>{currentRow.value=row}constcloseModal=()=>{currentRow.value=nullisModalOpen.value=false}constform=reactive({name:'',age:0,role:'',enable:true})// 修改字段form.name='ssy'form.age=18form.enable=false// 初始化 / 填充表单Object.assign(form,res.data)form={name:'new'}// ❌const{name}=form// ❌constcopy=JSON.parse(JSON.stringify(form))// ❌.value:count++// ❌const{name}=formRef.value// ❌解决方法:使用toRef或toRefs
constform=reactive({name:'',age:18})constname=toRef(form,'name')const{name,age}=toRefs(form)默认 const,因为 ref / reactive 内部的值是可变的,不需要重新赋值变量名
使用 let的场景:
lettimer:number|null=nulltimer=setTimeout(...)// 状态constloading=ref(false)constlist=ref<Item[]>([])constcurrentRow=ref<Row|null>(null)// 表单 / 模型constform=reactive({name:'',age:0,role:''})