响应网站建设,门户网站微信服务号建设方案,visual studio 网站开发,3分钟搞定网站seo优化外链建设一、ref创建基本类型的响应式数据 vue3可以使用ref、reactive去定义响应式数数据。 知识点汇总
使用ref需要先引入ref#xff0c;import {ref} from vue在模板 template 中使用了添加ref 的响应式数据#xff0c;变量的后面不用添加.value所有js代码里面#xff0c;去操作r…一、ref创建基本类型的响应式数据 vue3可以使用ref、reactive去定义响应式数数据。 知识点汇总
使用ref需要先引入refimport {ref} from vue在模板 template 中使用了添加ref 的响应式数据变量的后面不用添加.value所有js代码里面去操作res包裹的东西必须要加上 .value使用ref包裹的变量都变成了对象其中不带下划线的value 是提供我们自己使用的。ref可以定义基本类型、对象类型的响应式数据。 这个value中就是真正的变量数据。 Person.vue组件中的代码
templatediv classperson!-- 模板里面不用 name.value自动帮你加上.value --h2姓名{{name}}/h2h2年龄{{age}}/h2h2地址{{address}}/h2button clickchangeName修改名字/button button clickchangeAge增加年龄/buttonbutton clickshowTel查看联系方式/button/div
/templatescript setup langts namePerson//引入ref所有js代码里面去操作res包裹的东西必须要加上 .valueimport {ref} from vue //数据let name ref(张三) // 注意ref是函数let age ref(18) let tel 13888888888let address 河北 . 邯郸console.log(1,name)console.log(2,age)//方法function changeName(){name.value zhang-san console.log(1,name.value)} function changeAge(){age.value 1console.log(2,age.value) } function showTel(){alert(tel)}
/scriptstyle scoped.person{background-color: skyblue;box-shadow: 0 0 10px;border-radius: 10px;padding: 20px;}button{margin: 0 5px;}
/style二、reactive创建对象类型的响应式数据 reactive用于定义对象类型的数据 知识点汇总
使用reactive()可以把对象类型的数据变成响应式的。数组也是对象也可以使用reactive()去包裹数组。reactive定义的对象类型的响应式数据是深层次的。reactive只能定义对象类型的响应式数据。
templatediv classpersonh2一辆{{car.brand}}价值{{car.price}}万/h2button clickchangePrice修改汽车的价格/button hrul !-- “:” 就是把 “g.id” 当成js表达式去解析--li v-forg in games :keyg.id{{g.name}}/li/ulbutton clickchangeFirstGame修改第一个游戏的名字/buttonhrh2测试{{obj.a.b.c}}/h2button clickchangeC修改第一个 c 的数字/button/div
/templatescript setup langts namePerson//引入reactiveimport {reactive} from vue//使用 reactive 把car对象 变成响应式的//使用reactive(对象类型) 将对象类型的数据 变成响应式的let car reactive({brand:奔驰,price:100})console.log(car) //打印出来对象类型的数据存放在 Target 中//定义数组数组也是对象//使用reactive(数组) 将数组类型的数据 变成响应式的let games reactive([{id:hjhdjshj01,name:穿越火线},{id:hjhdjshj02,name:王者荣耀},{id:hjhdjshj03,name:原神},])//reactive定义的对象类型的响应式数据是深层次的let obj reactive({a:{b:{c:666}}})//方法function changePrice(){car.price 10}function changeFirstGame(){games[0].name 恐龙快打}//reactive定义的对象类型的响应式数据是深层次的function changeC(){obj.a.b.c 777}
/scriptstyle scoped.person{background-color: skyblue;box-shadow: 0 0 10px;border-radius: 10px;padding: 20px;}button{margin: 0 5px;}
/style三、ref创建对象类型的响应式数据
注意 使用ref处理对象类型的数据时在js代码中不要忘了加上 .value ref 处理对象类型的数据底层用的是reactive script setup langts namePersonlet car ref({brand:奔驰,price:100})let car1 reactive({brand:奔驰,price:100})console.log(car)console.log(car1)
/scripttemplatediv classpersonh2一辆{{car.brand}}价值{{car.price}}万/h2button clickchangePrice修改汽车的价格/button hrul li v-forg in games :keyg.id{{g.name}}/li/ulbutton clickchangeFirstGame修改第一个游戏的名字/button/div
/templatescript setup langts namePerson//引入 refimport {ref} from vue//使用 ref 把car对象 变成响应式的let car ref({brand:奔驰,price:100})//定义数组数组也是对象let games ref([{id:hjhdjshj01,name:穿越火线},{id:hjhdjshj02,name:王者荣耀},{id:hjhdjshj03,name:原神},])//方法function changePrice(){car.value.price 10}function changeFirstGame(){games.value[0].name 恐龙快打}/scriptstyle scoped.person{background-color: skyblue;box-shadow: 0 0 10px;border-radius: 10px;padding: 20px;}button{margin: 0 5px;}
/style四、ref对比reactive
通过Volar插件自动添加 .value 勾选上 Dot Value 如果sum是被 ref() 包裹的数据赋予的输入sum 便会自动填充.value 知识总结 reactive 重新分配一个新对象会失去响应式可以使用Object.assign去整体替换。Object.assign(obj1,obj2,obj3)表示把 obj2、obj3 中的每一组key-value组合都加在 obj1上obj1原先的内容会被覆盖。对于用 reactive 包裹的对象如果要整体修改对象需要使用Object.assign(obj1,obj2,obj3)如果用的是 ref 包裹的对象可以直接使用对象进行赋值。也是响应式的。ref 带 .value 必然是响应式的。 代码示例
templatediv classpersonh2一辆{{car.brand}}价值{{car.price}}万/h2button clickchangeBrand修改汽车的品牌/buttonbutton clickchangePrice修改汽车的价格/button button clickchangeCar修改汽车/button hrh2当前求和为{{sum}}/h2button clickchangeSum点我sum1/button/div
/templatescript setup langts namePerson//引入 ref、reactiveimport {ref,reactive} from vue//数据let car ref({brand:奔驰,price:100})let sum ref(0)//方法function changePrice(){car.value.price 10}function changeBrand(){car.value.brand 宝马}function changeCar(){// 对于 reactive 来说// car {brand:奥拓,price:1} //这样修改不了// car reactive({brand:奥拓,price:1}) //这样也是修改不了的。// 只有这种写法才能响应式的修改 car 对象中的内容// Object.assign(obj1,obj2,obj3)// 把 obj2、obj3 中的每一组key-value组合都加在 obj1obj1原先的内容会被覆盖// Object.assign(car,{brand:奥拓,price:1}) //reactive的写法// 如果用的是 ref 可以直接修改car.value {brand:奥拓,price:1} // ref 带 .value 必然是响应式的。}function changeSum(){sum.value 1// sum ref(9) //这样是不行的}/scriptstyle scoped.person{background-color: skyblue;box-shadow: 0 0 10px;border-radius: 10px;padding: 20px;}button{margin: 0 5px;}
/styleref 和 reactive 的使用建议
若需要一个基本类型的响应式数据必须使用ref。若需要一个响应式对象层级不深ref、reactive都可以。若需要一个响应式对象且层级较深推荐使用reactive。
五、toRefs与toRef toRefs()、toRef()就是把一个响应式对象给解构出来并且解构出来的数据也具备响应式。 代码示例
templatediv classpersonh2姓名{{name}}/h2h2年龄{{age}},{{nl}}/h2button clickchangeName修改姓名/buttonbutton clickchangeAge修改年龄/button/div
/templatescript setup langts namePersonimport {reactive,toRefs,toRef} from vue//数据let person reactive({name:张三,age:18 })//toRefs 的作用: 就是 把一个reactive定义的对象 变成 一个ref定义的对象//toRefs(person)此时的 name 和 age 就是响应式的了。let {name,age} toRefs(person)// let {name,age} person 相当于以下代码// let name person.name// let age person.age// 此时自定义的 name、age 不是响应式的 而 person.name、person.age 这两个是响应式//toRef// 第一个参数是响应式对象第二个参数是变量名let nl toRef(person,age)console.log(nl) //nl 也是一个 响应式 数据// 方法function changeName(){name.value ~//自定义 name 里面的值 和 person.name里面的值都会发生改变console.log(name.value,person.name)}function changeAge(){age.value 1console.log(age)}/scriptstyle scoped.person{background-color: skyblue;box-shadow: 0 0 10px;border-radius: 10px;padding: 20px;}button{margin: 0 5px;}
/stylepower by 尚硅谷
文章转载自: http://www.morning.ssrjt.cn.gov.cn.ssrjt.cn http://www.morning.mbpfk.cn.gov.cn.mbpfk.cn http://www.morning.qyxwy.cn.gov.cn.qyxwy.cn http://www.morning.jfwbr.cn.gov.cn.jfwbr.cn http://www.morning.wcgcm.cn.gov.cn.wcgcm.cn http://www.morning.cptzd.cn.gov.cn.cptzd.cn http://www.morning.dpplr.cn.gov.cn.dpplr.cn http://www.morning.fwdln.cn.gov.cn.fwdln.cn http://www.morning.sjgsh.cn.gov.cn.sjgsh.cn http://www.morning.zhishizf.cn.gov.cn.zhishizf.cn http://www.morning.tldhq.cn.gov.cn.tldhq.cn http://www.morning.tyklz.cn.gov.cn.tyklz.cn http://www.morning.dlurfdo.cn.gov.cn.dlurfdo.cn http://www.morning.gsjzs.cn.gov.cn.gsjzs.cn http://www.morning.hhboyus.cn.gov.cn.hhboyus.cn http://www.morning.mrskk.cn.gov.cn.mrskk.cn http://www.morning.stmkm.cn.gov.cn.stmkm.cn http://www.morning.tngdn.cn.gov.cn.tngdn.cn http://www.morning.knzdt.cn.gov.cn.knzdt.cn http://www.morning.dhtdl.cn.gov.cn.dhtdl.cn http://www.morning.ykxnp.cn.gov.cn.ykxnp.cn http://www.morning.rqqct.cn.gov.cn.rqqct.cn http://www.morning.kpcxj.cn.gov.cn.kpcxj.cn http://www.morning.pbpcj.cn.gov.cn.pbpcj.cn http://www.morning.syssdz.cn.gov.cn.syssdz.cn http://www.morning.jyknk.cn.gov.cn.jyknk.cn http://www.morning.rzcmn.cn.gov.cn.rzcmn.cn http://www.morning.jzgxp.cn.gov.cn.jzgxp.cn http://www.morning.lbggk.cn.gov.cn.lbggk.cn http://www.morning.bqhlp.cn.gov.cn.bqhlp.cn http://www.morning.wcqkp.cn.gov.cn.wcqkp.cn http://www.morning.tygn.cn.gov.cn.tygn.cn http://www.morning.zxgzp.cn.gov.cn.zxgzp.cn http://www.morning.bgkk.cn.gov.cn.bgkk.cn http://www.morning.kwhrq.cn.gov.cn.kwhrq.cn http://www.morning.yrjym.cn.gov.cn.yrjym.cn http://www.morning.bswxt.cn.gov.cn.bswxt.cn http://www.morning.pzdxg.cn.gov.cn.pzdxg.cn http://www.morning.pbtrx.cn.gov.cn.pbtrx.cn http://www.morning.cknsx.cn.gov.cn.cknsx.cn http://www.morning.fgppj.cn.gov.cn.fgppj.cn http://www.morning.pzbqm.cn.gov.cn.pzbqm.cn http://www.morning.wxccm.cn.gov.cn.wxccm.cn http://www.morning.tnjz.cn.gov.cn.tnjz.cn http://www.morning.cgmzt.cn.gov.cn.cgmzt.cn http://www.morning.dpzcc.cn.gov.cn.dpzcc.cn http://www.morning.lonlie.com.gov.cn.lonlie.com http://www.morning.zknxh.cn.gov.cn.zknxh.cn http://www.morning.gkdhf.cn.gov.cn.gkdhf.cn http://www.morning.ljdhj.cn.gov.cn.ljdhj.cn http://www.morning.bwqr.cn.gov.cn.bwqr.cn http://www.morning.clhyj.cn.gov.cn.clhyj.cn http://www.morning.xmjzn.cn.gov.cn.xmjzn.cn http://www.morning.kchwr.cn.gov.cn.kchwr.cn http://www.morning.kyjpg.cn.gov.cn.kyjpg.cn http://www.morning.xpzkr.cn.gov.cn.xpzkr.cn http://www.morning.ylsxk.cn.gov.cn.ylsxk.cn http://www.morning.phnbd.cn.gov.cn.phnbd.cn http://www.morning.dygqq.cn.gov.cn.dygqq.cn http://www.morning.bzgpj.cn.gov.cn.bzgpj.cn http://www.morning.xhftj.cn.gov.cn.xhftj.cn http://www.morning.nyqxy.cn.gov.cn.nyqxy.cn http://www.morning.jzxqj.cn.gov.cn.jzxqj.cn http://www.morning.dycbp.cn.gov.cn.dycbp.cn http://www.morning.mkrjf.cn.gov.cn.mkrjf.cn http://www.morning.yrrnx.cn.gov.cn.yrrnx.cn http://www.morning.tfzjl.cn.gov.cn.tfzjl.cn http://www.morning.yhglt.cn.gov.cn.yhglt.cn http://www.morning.fwqgy.cn.gov.cn.fwqgy.cn http://www.morning.wqfj.cn.gov.cn.wqfj.cn http://www.morning.fjshyc.com.gov.cn.fjshyc.com http://www.morning.jkpnm.cn.gov.cn.jkpnm.cn http://www.morning.wpjst.cn.gov.cn.wpjst.cn http://www.morning.wrbx.cn.gov.cn.wrbx.cn http://www.morning.lkbyj.cn.gov.cn.lkbyj.cn http://www.morning.hxmqb.cn.gov.cn.hxmqb.cn http://www.morning.gmwqd.cn.gov.cn.gmwqd.cn http://www.morning.xplng.cn.gov.cn.xplng.cn http://www.morning.lfcfn.cn.gov.cn.lfcfn.cn http://www.morning.rltw.cn.gov.cn.rltw.cn