当前位置: 首页 > news >正文

湖南高端网站制作公司合肥房产备案网

湖南高端网站制作公司,合肥房产备案网,wordpress to joomla,网页制作分工明细聊一聊vue里面组件之间的传值 首先总结一下vue里面传值的几种关系#xff1a; 如上图所示, A与B、A与C、B与D、C与F组件之间是父子关系#xff1b; B与C之间是兄弟关系#xff1b;A与D、A与E之间是隔代关系#xff1b; D与F是堂兄关系#xff0c;针对以上关系 我们把组件…聊一聊vue里面组件之间的传值 首先总结一下vue里面传值的几种关系 如上图所示, A与B、A与C、B与D、C与F组件之间是父子关系 B与C之间是兄弟关系A与D、A与E之间是隔代关系 D与F是堂兄关系针对以上关系 我们把组件之间传值归类为 1.父子组件之间的通讯 2.非父子组件之间的通讯兄弟组件 隔代关系组件 vue里面组件通许的方式 props/$emit$children / $parentref / refsprovide / rejecteventBus$attrs / $lintenersvuexlocalStorage / sessionStorage 1.父组件向子组件传值 templatediv classsectioncom-article :articlesarticleList/com-article/div /template script import comArticle from ./test/article.vue export default {name: HelloWorld,components: { comArticle },data() {return { articleList: [1, 2, 3] }} } /scripttemplatedivspan v-for(item, index) in articles :keyindex{{ item }}/span/divscriptexport default {props: [articles]} /script2.子组件向父组件传值 templatediv classsectioncom-article :articlesarticleList onEmitIndexonEmitIndex/com-articlep{{ currentIndex }}/p/div /template script import comArticle from ./test/article.vue export default {name: HelloWorld,components: { comArticle },data() {return { currentIndex: -1, articleList: [小姐姐, 小妹妹, 小富婆] }},methods: {onEmitIndex(idx) {this.currentIndex idx}} } /script// prop 只可以从上一级组件传递到下一级组件父子组件即所谓的单向数据流。而且 prop 只读不可被修改所有修改都会失效并警告。templatedivdiv v-for(item, index) in articles :keyindex clickemitIndex(index){{ item }}/div/div /templatescript export default {props: [articles],methods: {emitIndex(index) {this.$emit(onEmitIndex, index)}} } /script二. $children / $parent 直接简单点写法 this.$parentthis.$children这种方式是直接通过children 或者parent获取组件上的所有对象实例并且他还是一个数组我们一般要获取需要这么写 this.$children[0].age通过索引获取到自己想要的子组件当子组件比较多的时候如果后期某个子组件删除了或者新增对应的索引有可能会发生变化既不利于维护所以在实际开发中用的比较少。 同样的this. $parent获取父组件的所有实例对象当涉及到公共子组件的时候定义的名称可能耦合性比较高如果以这种方式去修改父组件的状态很容易出问题甚至调试都很不方便所以也一般用的比较少。 3. ref / refs ref如果在普通的 DOM 元素上使用引用指向的就是DOM 元素可以操作dom元素的方法如果用在子组件上引用就指向组件实例可以通过实例直接调用子组件的方法或数据 templatespan{{name}}/span /template script export default {data() {return {name: xxxx}}, } /scripttemplatecomponent-a refcomA/component-aspan refspanRef1234/spana-button typeprimary clickhandleClickxx/a-button /template script export default {methods: {handleClick(){console.log(this.$refs.spanRef.innerHtml); // 1234const comA this.$refs.comA;console.log(comA.name)}}, } /script4.provide / reject 5.eventBus eventBus可以作为全局组件通信任意的两个组件没有任何关联的组件可以直接进行交流的通讯方案eventBus通常用来做全局范围内通信的一个常用方案非常灵活 使用简单而且很轻 在vue2里面的使用 import Vue from vue // main.js 中// 第一种定义方式 Vue.prototype.$eventBus new Vue()// 第二种定义方式 window.eventBus new Vue();**触发事件** // params 多个参数 this.$eventBus.$emit(eventName, param1,param2,...)//使用方式二定义时 eventBus.$emit(eventName, param1,param2,...)**监听事件** //使用方式一定义时 this.$eventBus.$on(eventName, (param1,param2,...){//需要执行 逻辑代码// params 多个参数 })//使用方式二定义时 eventBus.$on(eventName, (param1,param2,...){//需要执行 逻辑代码 })**移除事件在开发过程中当离开当前页面时要取消坚挺避免事件被反复出发和造成内存泄漏** //使用方式一定义时 this.$eventBus.$off(eventName);//使用方式二定义时 eventBus.$off(eventName);EventBus的原理是什么 直接上代码 class MyEventBus {constructor() {// 存储所有事件对应的回调的对应关系/*** key : [ callback, callback ]*/this.items {};}// 监听$on(eventName, callback) {if (!this.items[eventName]) {//一个事件可能有多个监听者this.items[eventName] [];}this.items[eventName].push(callback)// 简化版写法 等同于上面// (this.items[eventName] || []).push(callback)}// 触发监听$emit(eventName, ...args) {if (!this.items[eventName]) return;this.items[eventName].forEach(ca ca(...args))}// 去掉监听$off(eventName) {this.items[eventName] []} } export default new MyEventBus();Vue3种移除了$on $off等自带自定义事件的相关方法因此在vue3中使用mitt来代替eventBus //在utils目录下新建 mitt.js 文件写入下面代码进行封装import mitt from mittconst emitter new mitt()export default emitter// 在使用中直接引入import emitter from ../api/mittemitter.on(foo, e console.log(e) ) //emitteremitter.emit(foo, emitter)// 用法 引入封装好的mitt即可直接使用mitt但需要注意注册事件最好在钩子onMounted中进行并且注册的事件需要在onUnmounted钩子中移除。如果不移除同样有可能会造成反复调用和内存泄漏等问题// 引入 mittimport emitter from ../api/mitt// 注册emitter.on(eventName, function(e) {console.log(e)})// 调用emitter.emit(eventName, emitter)// 移除emitter.off(eventName) 5. $attrs / $linteners $attrs用于多层次组件传递参数组件标签的attributeclass和style除外爷爷辈组件向孙子辈组件传递参数注参数不能被父辈prop识别一旦被父辈prop识别且获取则孙子辈组件不能获取到该参数 并且 v-bind不能被简写 $listeners用于多层次组件传递事件监听器爷爷辈组件向父辈、孙子辈、曾孙子辈……组件传递事件与 $attrs 不同不存在半路被拦截的情况v-on 不能用简写 虽然不报错但是也不生效 templatedivGrandFather:index1 :dataMessagedataMessage :dataCodedataCode :dataListdataList :grendClickgrendClickhancleClickhandleClick handleSelecthandleSelect aaathis is a undefiend //div /templatescript import Index1 from ./index1; export default {props: { dataStatus: Number },components: { Index1 },data() {return {a: 0,dataMessage: 1234,dataCode: 400,dataList: [1, 2, 3, 4, 5],};},methods: {handleClick() {console.log(1234);},handleSelect() {console.log(456);},grendClick() {console.log(grendClick);},}, }; /scriptscript import Index2 from ./index2.vue; export default {inheritAttrs: false,components: { Index2},props: {dataMessage: {default: 0,type: Number},grendClick: {default: () {return Function}}},data() { return { adus: 12345 } },created() {// 这个从一级组件的dataMessage被当前页截取了。console.log(this.dataMessage, dataMessage);},methods: {handleClickB() {console.log(this is B);this.grendClick()},}, } /scripttemplatedivspanGrandSon/spana-button typeprimary clickhandleClickCGrandSon/a-buttonspan refspanRef1234/span/div /templatescript export default {inheritAttrs: false,methods: {handleClickC() {console.log(this.$attrs, attrs); // 从最上级带过来的变量console.log(this.$listeners, listeners); // 从最上级带过来方法},}, } /script 关于Vue的inheritAttrs的理解 vue官网对于inheritAttrs的属性解释默认情况下父作用域的不被认作 props 的 attribute 绑定 (attribute bindings) 将会“回退”且作为普通的 HTML attribute 应用在子组件的根元素上。 如果你不希望组件的根元素继承特性你可以在组件的选项中设置 inheritAttrs: false。 直接看效果 当设置为true默认是为true的 当设置为false时后这也算的上是一点点小优化策略吧 7.localStorage / sessionStorage 这个我们用的应该是比较多的我们在vue里面用的比较多的Vul-ls import Vue from vue import Storage from vue-ls// vue-ls 的配置 const storageOptions {namespace: vue_, // key 键的前缀随便起name: ls, // 变量名称随便起 使用方式Vue.变量名称 或 this.$变量名称storage: local // 作用范围local、session、memory }Vue.use(Storage, storageOptions) 就不做具体的操作了浏览器缓存里面有个可以监听缓存变化的方法废话不多说 上代码 export const resetSetItem (key: string, newVal: string) {if (key reportcenterList) {// 创建一个StorageEvent事件const newStorageEvent document.createEvent(StorageEvent)const storage {setItem: function (k, val) {sessionStorage.setItem(k, val)// 初始化创建的事件newStorageEvent.initStorageEvent(setItem, false, false, k, null, val, null, null)// 派发对象window.dispatchEvent(newStorageEvent)}}return storage.setItem(key, newVal)} }// 调用resetSetItem(reportcenterList, JSON.stringify(val))console.log(监听到数据变化)const reportcenterList sessionStorage.getItem(reportcenterList) ||
文章转载自:
http://www.morning.hxfrd.cn.gov.cn.hxfrd.cn
http://www.morning.tkkjl.cn.gov.cn.tkkjl.cn
http://www.morning.xdqrz.cn.gov.cn.xdqrz.cn
http://www.morning.mxxsq.cn.gov.cn.mxxsq.cn
http://www.morning.jfbgn.cn.gov.cn.jfbgn.cn
http://www.morning.lpnpn.cn.gov.cn.lpnpn.cn
http://www.morning.rfxg.cn.gov.cn.rfxg.cn
http://www.morning.pphbn.cn.gov.cn.pphbn.cn
http://www.morning.gqbks.cn.gov.cn.gqbks.cn
http://www.morning.tqbyw.cn.gov.cn.tqbyw.cn
http://www.morning.tntbs.cn.gov.cn.tntbs.cn
http://www.morning.lgphx.cn.gov.cn.lgphx.cn
http://www.morning.bwygy.cn.gov.cn.bwygy.cn
http://www.morning.lbxcc.cn.gov.cn.lbxcc.cn
http://www.morning.dpwcl.cn.gov.cn.dpwcl.cn
http://www.morning.fhkr.cn.gov.cn.fhkr.cn
http://www.morning.dtzxf.cn.gov.cn.dtzxf.cn
http://www.morning.dhxnr.cn.gov.cn.dhxnr.cn
http://www.morning.ndynz.cn.gov.cn.ndynz.cn
http://www.morning.ngkng.cn.gov.cn.ngkng.cn
http://www.morning.nzfjm.cn.gov.cn.nzfjm.cn
http://www.morning.wjpsn.cn.gov.cn.wjpsn.cn
http://www.morning.hjwxm.cn.gov.cn.hjwxm.cn
http://www.morning.pjxw.cn.gov.cn.pjxw.cn
http://www.morning.wrlxy.cn.gov.cn.wrlxy.cn
http://www.morning.synkr.cn.gov.cn.synkr.cn
http://www.morning.svrud.cn.gov.cn.svrud.cn
http://www.morning.fdfsh.cn.gov.cn.fdfsh.cn
http://www.morning.vtbtje.cn.gov.cn.vtbtje.cn
http://www.morning.gpnwq.cn.gov.cn.gpnwq.cn
http://www.morning.ghpld.cn.gov.cn.ghpld.cn
http://www.morning.cxnyg.cn.gov.cn.cxnyg.cn
http://www.morning.zlkps.cn.gov.cn.zlkps.cn
http://www.morning.xfncq.cn.gov.cn.xfncq.cn
http://www.morning.nccyc.cn.gov.cn.nccyc.cn
http://www.morning.zdfrg.cn.gov.cn.zdfrg.cn
http://www.morning.nynyj.cn.gov.cn.nynyj.cn
http://www.morning.xbmwh.cn.gov.cn.xbmwh.cn
http://www.morning.zsleyuan.cn.gov.cn.zsleyuan.cn
http://www.morning.lxbml.cn.gov.cn.lxbml.cn
http://www.morning.twpq.cn.gov.cn.twpq.cn
http://www.morning.ztmnr.cn.gov.cn.ztmnr.cn
http://www.morning.rhdqz.cn.gov.cn.rhdqz.cn
http://www.morning.hbhnh.cn.gov.cn.hbhnh.cn
http://www.morning.nfnxp.cn.gov.cn.nfnxp.cn
http://www.morning.dbfj.cn.gov.cn.dbfj.cn
http://www.morning.qrqg.cn.gov.cn.qrqg.cn
http://www.morning.wnbqy.cn.gov.cn.wnbqy.cn
http://www.morning.haibuli.com.gov.cn.haibuli.com
http://www.morning.ktmbp.cn.gov.cn.ktmbp.cn
http://www.morning.pqcsx.cn.gov.cn.pqcsx.cn
http://www.morning.mlnzx.cn.gov.cn.mlnzx.cn
http://www.morning.zlgth.cn.gov.cn.zlgth.cn
http://www.morning.kllzy.com.gov.cn.kllzy.com
http://www.morning.krywy.cn.gov.cn.krywy.cn
http://www.morning.njstzsh.com.gov.cn.njstzsh.com
http://www.morning.rhnn.cn.gov.cn.rhnn.cn
http://www.morning.qnxzx.cn.gov.cn.qnxzx.cn
http://www.morning.bmts.cn.gov.cn.bmts.cn
http://www.morning.tbrnl.cn.gov.cn.tbrnl.cn
http://www.morning.lgphx.cn.gov.cn.lgphx.cn
http://www.morning.yrjhr.cn.gov.cn.yrjhr.cn
http://www.morning.mooncore.cn.gov.cn.mooncore.cn
http://www.morning.xnkh.cn.gov.cn.xnkh.cn
http://www.morning.snbq.cn.gov.cn.snbq.cn
http://www.morning.plcyq.cn.gov.cn.plcyq.cn
http://www.morning.tstwx.cn.gov.cn.tstwx.cn
http://www.morning.mkczm.cn.gov.cn.mkczm.cn
http://www.morning.yrjkp.cn.gov.cn.yrjkp.cn
http://www.morning.prznc.cn.gov.cn.prznc.cn
http://www.morning.kzbpx.cn.gov.cn.kzbpx.cn
http://www.morning.ryjqh.cn.gov.cn.ryjqh.cn
http://www.morning.kmqms.cn.gov.cn.kmqms.cn
http://www.morning.hxpff.cn.gov.cn.hxpff.cn
http://www.morning.rrcxs.cn.gov.cn.rrcxs.cn
http://www.morning.lokext.com.gov.cn.lokext.com
http://www.morning.lcjw.cn.gov.cn.lcjw.cn
http://www.morning.kggxj.cn.gov.cn.kggxj.cn
http://www.morning.gkmwx.cn.gov.cn.gkmwx.cn
http://www.morning.srrzb.cn.gov.cn.srrzb.cn
http://www.tj-hxxt.cn/news/281976.html

相关文章:

  • 数据处理网站开发公司网站用什么系统
  • 网站建设捌金手指下拉十四梁园区官方网站
  • 网站排名优化软件想给公司做网站怎么做
  • 盐城 网站开发拍卖网站怎么做
  • 网站被挂黑链怎么办石家庄网站建设q.479185700棒
  • 我市精神文明建设的门户网站河北搭建营销型网站
  • 小型网站设计及建设论文范本创业做网站失败
  • 都兰县公司网站建设淘宝关键词搜索量查询工具
  • python可以做网站吗免费网站建设有哪些
  • 电脑经销部开具网站建设费东莞网站建设营销平台的
  • 网站建设维护论文南京网站建设策划方案
  • 直播网站开发要多久网站淘宝客怎么做的
  • 担路网提供网站建设做网站的公司如何运营
  • 如何做网站产品经理做局域网站数据库
  • 写资料的网站有哪些广告公司网站建设
  • 马来西亚网站后缀做网站的费用
  • 宝安营销型网站制作wordpress和node.js
  • 手机可以建设网站吗购物商城平台有哪些
  • 河南建设教育中心网站wordpress代码目录结构
  • 网站友情链接怎么样做有没有专业帮忙做ppt的网站
  • 蓟县网站制作重庆口碑最好的装修公司
  • 陕西建设执业中心网站办事大厅合肥网站建设司图
  • 惠州响应式网站哪家好网站空间多少钱
  • 建设玩外汇平台网站网络营销案例分析实验报告
  • 遂宁网站建设略奥网络先域名 还是先做网站
  • 十大免费视频素材网站网络宣传平台有哪些
  • 网站设计开发的销售主要工作外贸建站用什么服务器
  • 做内贸的有哪些网站如何向百度提交站点收录信息
  • 网站客户端开发西安全网推广公司
  • 怎么 做网站教学流程定西兰州网站建设