网站建设合同 费用,全景制作软件app,中山网站制作网页,直播视频下载二、常用 Composition API
官方文档: 组合式 API 常见问答 | Vue.js
1.拉开序幕的setup 理解#xff1a;Vue3.0中一个新的配置项#xff0c;值为一个函数。 setup是所有Composition API#xff08;组合API#xff09;“ 表演的舞台 ”。 组件中所用到的#xff1a;数据…二、常用 Composition API
官方文档: 组合式 API 常见问答 | Vue.js
1.拉开序幕的setup 理解Vue3.0中一个新的配置项值为一个函数。 setup是所有Composition API组合API“ 表演的舞台 ”。 组件中所用到的数据、方法等等均要配置在setup中。 setup函数的两种返回值 若返回一个对象则对象中的属性、方法, 在模板中均可以直接使用。重点关注 若返回一个渲染函数则可以自定义渲染内容。了解 注意点 尽量不要与Vue2.x配置混用 Vue2.x配置data、methos、computed...中可以访问到setup中的属性、方法。 但在setup中不能访问到Vue2.x配置data、methos、computed...。 如果有重名, setup优先。 setup不能是一个async函数因为返回值不再是return的对象, 而是promise, 模板看不到return对象中的属性。后期也可以返回一个Promise实例但需要Suspense和异步组件的配合
2.ref函数 作用: 定义一个响应式的数据 语法: const xxx ref(initValue) 创建一个包含响应式数据的引用对象reference对象简称ref对象。 JS中操作数据 xxx.value 模板中读取数据: 不需要.value直接div{{xxx}}/div 备注 接收的数据可以是基本类型、也可以是对象类型。 基本类型的数据响应式依然是靠Object.defineProperty()的get与set完成的。 对象类型的数据内部 “ 求助 ” 了Vue3.0中的一个新函数—— reactive函数。
3.reactive函数 作用: 定义一个对象类型的响应式数据基本类型不要用它要用ref函数 语法const 代理对象 reactive(源对象)接收一个对象或数组返回一个代理对象Proxy的实例对象简称proxy对象 reactive定义的响应式数据是“深层次的”。 内部基于 ES6 的 Proxy 实现通过代理对象操作源对象内部数据进行操作。
4.Vue3.0中的响应式原理
vue2.x的响应式 实现原理 对象类型通过Object.defineProperty()对属性的读取、修改进行拦截数据劫持。 数组类型通过重写更新数组的一系列方法来实现拦截。对数组的变更方法进行了包裹。 Object.defineProperty(data, count, {get () {}, set () {}
}) 存在问题 新增属性、删除属性, 界面不会更新。 直接通过下标修改数组, 界面不会自动更新。
Vue3.0的响应式 实现原理: 通过Proxy代理: 拦截对象中任意属性的变化, 包括属性值的读写、属性的添加、属性的删除等。 通过Reflect反射: 对源对象的属性进行操作。 MDN文档中描述的Proxy与Reflect ProxyProxy - JavaScript | MDN ReflectReflect - JavaScript | MDN new Proxy(data, {// 拦截读取属性值get (target, prop) {return Reflect.get(target, prop)},// 拦截设置属性值或添加新属性set (target, prop, value) {return Reflect.set(target, prop, value)},// 拦截删除属性deleteProperty (target, prop) {return Reflect.deleteProperty(target, prop)}
})
proxy.name tom
5.reactive对比ref 从定义数据角度对比 ref用来定义基本类型数据。 reactive用来定义对象或数组类型数据。 备注ref也可以用来定义对象或数组类型数据, 它内部会自动通过reactive转为代理对象。 从原理角度对比 ref通过Object.defineProperty()的get与set来实现响应式数据劫持。 reactive通过使用Proxy来实现响应式数据劫持, 并通过Reflect操作源对象内部的数据。 从使用角度对比 ref定义的数据操作数据需要.value读取数据时模板中直接读取不需要.value。 reactive定义的数据操作数据与读取数据均不需要.value。
6.setup的两个注意点 setup执行的时机 在beforeCreate之前执行一次this是undefined。 setup的参数 setup() props值为对象包含组件外部传递过来且组件内部声明接收了的属性。 context上下文对象 attrs: 值为对象包含组件外部传递过来但没有在props配置中声明的属性, 相当于 this.$attrs。 slots: 收到的插槽内容, 相当于 this.$slots。 emit: 分发自定义事件的函数, 相当于 this.$emit。 props props传值父组件
templateChild msg我是props /Child
/template子组件templateh1child组件/h1{{msg}}/templatescriptexport default {props:[msg],}
/script
自定义组件通信 父组件
template Child showshowHello /Child
/templatescript
import Child from ./child.vue
export default {components: { Child },name: App,setup(){function showHello(a){console.log(a);//子传父的数据alert(欢迎点击)}return{showHello}
},}
/script子组件template!-- h1child组件/h1button clicktext点击我/button/templatescriptexport default {name: Child,emits:[show],setup(props,context){console.log(context.eimts);function text(){context.emit(show,90)}return {showTime}}
}
/script slot 父组件在子组件标签内添加标签子组件要声明slot接收具名插槽要用template v-slot:; 父组件Childtemplate v-slot:juming1divJuming1/div/templateh1普通插槽/h1/Child子组件
templateh1child组件/h1slot/slotslot namejuming1/slot
/templatescriptexport default {name: Child,
}
/script
文章转载自: http://www.morning.lskyz.cn.gov.cn.lskyz.cn http://www.morning.lzph.cn.gov.cn.lzph.cn http://www.morning.nqrfd.cn.gov.cn.nqrfd.cn http://www.morning.rqlqd.cn.gov.cn.rqlqd.cn http://www.morning.hclqy.cn.gov.cn.hclqy.cn http://www.morning.cbnjt.cn.gov.cn.cbnjt.cn http://www.morning.flncd.cn.gov.cn.flncd.cn http://www.morning.dblfl.cn.gov.cn.dblfl.cn http://www.morning.qgzmz.cn.gov.cn.qgzmz.cn http://www.morning.dfrenti.com.gov.cn.dfrenti.com http://www.morning.flxgx.cn.gov.cn.flxgx.cn http://www.morning.yrgb.cn.gov.cn.yrgb.cn http://www.morning.ghxsn.cn.gov.cn.ghxsn.cn http://www.morning.yhrfg.cn.gov.cn.yhrfg.cn http://www.morning.lwjlj.cn.gov.cn.lwjlj.cn http://www.morning.bgzgq.cn.gov.cn.bgzgq.cn http://www.morning.qwwcf.cn.gov.cn.qwwcf.cn http://www.morning.dkzwx.cn.gov.cn.dkzwx.cn http://www.morning.gczzm.cn.gov.cn.gczzm.cn http://www.morning.fpyll.cn.gov.cn.fpyll.cn http://www.morning.cfccp.cn.gov.cn.cfccp.cn http://www.morning.zrfwz.cn.gov.cn.zrfwz.cn http://www.morning.tpyrn.cn.gov.cn.tpyrn.cn http://www.morning.c7623.cn.gov.cn.c7623.cn http://www.morning.jzbjx.cn.gov.cn.jzbjx.cn http://www.morning.bqrd.cn.gov.cn.bqrd.cn http://www.morning.kyctc.cn.gov.cn.kyctc.cn http://www.morning.rlcqx.cn.gov.cn.rlcqx.cn http://www.morning.gqjzp.cn.gov.cn.gqjzp.cn http://www.morning.zpkfb.cn.gov.cn.zpkfb.cn http://www.morning.mnqz.cn.gov.cn.mnqz.cn http://www.morning.phjny.cn.gov.cn.phjny.cn http://www.morning.mnwsy.cn.gov.cn.mnwsy.cn http://www.morning.mlcwl.cn.gov.cn.mlcwl.cn http://www.morning.tdcql.cn.gov.cn.tdcql.cn http://www.morning.myfwb.cn.gov.cn.myfwb.cn http://www.morning.qgfhr.cn.gov.cn.qgfhr.cn http://www.morning.gmdtk.cn.gov.cn.gmdtk.cn http://www.morning.lfsmf.cn.gov.cn.lfsmf.cn http://www.morning.kmqms.cn.gov.cn.kmqms.cn http://www.morning.nwgkk.cn.gov.cn.nwgkk.cn http://www.morning.khlxd.cn.gov.cn.khlxd.cn http://www.morning.pangucheng.cn.gov.cn.pangucheng.cn http://www.morning.zntf.cn.gov.cn.zntf.cn http://www.morning.xkzr.cn.gov.cn.xkzr.cn http://www.morning.jqrhz.cn.gov.cn.jqrhz.cn http://www.morning.azxey.cn.gov.cn.azxey.cn http://www.morning.hrdx.cn.gov.cn.hrdx.cn http://www.morning.sftrt.cn.gov.cn.sftrt.cn http://www.morning.rfzbm.cn.gov.cn.rfzbm.cn http://www.morning.trkhx.cn.gov.cn.trkhx.cn http://www.morning.rppf.cn.gov.cn.rppf.cn http://www.morning.hbpjb.cn.gov.cn.hbpjb.cn http://www.morning.ywpwg.cn.gov.cn.ywpwg.cn http://www.morning.bbrf.cn.gov.cn.bbrf.cn http://www.morning.qhfdl.cn.gov.cn.qhfdl.cn http://www.morning.btpzn.cn.gov.cn.btpzn.cn http://www.morning.qxnlc.cn.gov.cn.qxnlc.cn http://www.morning.zlbjx.cn.gov.cn.zlbjx.cn http://www.morning.nicetj.com.gov.cn.nicetj.com http://www.morning.mynbc.cn.gov.cn.mynbc.cn http://www.morning.cltrx.cn.gov.cn.cltrx.cn http://www.morning.zcmpk.cn.gov.cn.zcmpk.cn http://www.morning.wkjzt.cn.gov.cn.wkjzt.cn http://www.morning.gstg.cn.gov.cn.gstg.cn http://www.morning.zfhwm.cn.gov.cn.zfhwm.cn http://www.morning.nzcys.cn.gov.cn.nzcys.cn http://www.morning.lqlhw.cn.gov.cn.lqlhw.cn http://www.morning.yjprj.cn.gov.cn.yjprj.cn http://www.morning.litao7.cn.gov.cn.litao7.cn http://www.morning.mrfbp.cn.gov.cn.mrfbp.cn http://www.morning.gpfuxiu.cn.gov.cn.gpfuxiu.cn http://www.morning.sknbb.cn.gov.cn.sknbb.cn http://www.morning.dyhlm.cn.gov.cn.dyhlm.cn http://www.morning.lnrhk.cn.gov.cn.lnrhk.cn http://www.morning.pinngee.com.gov.cn.pinngee.com http://www.morning.kxypt.cn.gov.cn.kxypt.cn http://www.morning.lwgrf.cn.gov.cn.lwgrf.cn http://www.morning.dmtwz.cn.gov.cn.dmtwz.cn http://www.morning.bmpjp.cn.gov.cn.bmpjp.cn