vue项目前端知识点整理【收藏】(2)

_that.socket.onerror = evt => { if (!_that.page.beginTime) { _that.$vux.toast.text('网络忙,请稍后重试') return false } // 不重连 if (this.noConnection == true) { return false } // socket断线重连 var date = new Date().getTime() // 判断断线时间是否超过60秒 if (date - _that.openTime > 60000) { _that.reconnection = true _that.createSocket() } }

发送音频时第一次授权问题

发送音频时,第一次点击会弹框提示授权,不管点击允许还是拒绝都会执行 wx.startRecord() ,这样再次调用录音就会出现问题(因为上一个录音没有结束), 由于录音方法是由 touchstart 事件触发的,可以使用 touchcancel 事件捕获弹出提示授权的状态。

_that.$refs.btnVoice.addEventListener("touchcancel" ,function(event) { event.preventDefault() // 手动触发 touchend _that.voice.isUpload = false _that.voice.voiceText = '按住 说话' _that.voice.touchStart = false _that.stopRecord() })

组件销毁时,没有清空定时器

在组件实例被销毁后, setInterval() 还会继续执行,需要手动清除,否则会占用内存。

mounted(){ this.timer = (() => { ... }, 1000) }, //最后在beforeDestroy()生命周期内清除定时器 beforeDestroy() { clearInterval(this.timer) this.timer = null }

watch监听对象的变化

watch: { chatList: { deep: true, // 监听对象的变化 handler: function (newVal,oldVal){ ... } } }

后台管理系统模板问题

由于后台管理系统增加了菜单权限,路由是根据菜单权限动态生成的,当只有一个菜单的权限时,会导致这个菜单可能不显示,参看模板的源码:

<router-link v-if="hasOneShowingChildren(item.children) && !item.children[0].children&&!item.alwaysShow" :to="resolvePath(item.children[0].path)"> <el-menu-item :index="resolvePath(item.children[0].path)" :class="{'submenu-title-noDropdown':!isNest}"> <svg-icon v-if="item.children[0].meta&&item.children[0].meta.icon" :icon-class="item.children[0].meta.icon"></svg-icon> <span v-if="item.children[0].meta&&item.children[0].meta.title" slot="title">{{generateTitle(item.children[0].meta.title)}}</span> </el-menu-item> </router-link> <el-submenu v-else :index="item.name||item.path"> <template slot="title"> <svg-icon v-if="item.meta&&item.meta.icon" :icon-class="item.meta.icon"></svg-icon> <span v-if="item.meta&&item.meta.title" slot="title">{{generateTitle(item.meta.title)}}</span> </template> <template v-for="child in item.children" v-if="!child.hidden"> <sidebar-item :is-nest="true" v-if="child.children&&child.children.length>0" :item="child" :key="child.path" :base-path="resolvePath(child.path)"></sidebar-item> <router-link v-else :to="resolvePath(child.path)" :key="child.name"> <el-menu-item :index="resolvePath(child.path)"> <svg-icon v-if="child.meta&&child.meta.icon" :icon-class="child.meta.icon"></svg-icon> <span v-if="child.meta&&child.meta.title" slot="title">{{generateTitle(child.meta.title)}}</span> </el-menu-item> </router-link> </template> </el-submenu>

其中 v-if="hasOneShowingChildren(item.children) && !item.children[0].children&&!item.alwaysShow" 表示当这个节点只有一个子元素,且这个节点的第一个子元素没有子元素时,显示一个特殊的菜单样式。而问题是 item.children[0] 可能是一个隐藏的菜单( item.hidden === true ),所以当这个表达式成立时,可能会渲染一个隐藏的菜单。参看最新的后台源码,作者已经修复了这个问题。

<template v-if="hasOneShowingChild(item.children,item) && (!onlyOneChild.children||onlyOneChild.noShowingChildren)&&!item.alwaysShow"> <app-link v-if="onlyOneChild.meta" :to="resolvePath(onlyOneChild.path)"> <el-menu-item :index="resolvePath(onlyOneChild.path)" :class="{'submenu-title-noDropdown':!isNest}"> <item :icon="onlyOneChild.meta.icon||(item.meta&&item.meta.icon)" :title="onlyOneChild.meta.title" /> </el-menu-item> </app-link> </template>methods: { hasOneShowingChild(children = [], parent) { const showingChildren = children.filter(item => { if (item.hidden) { return false } else { // Temp set(will be used if only has one showing child) this.onlyOneChild = item return true } }) // When there is only one child router, the child router is displayed by default if (showingChildren.length === 1) { return true } // Show parent if there are no child router to display if (showingChildren.length === 0) { this.onlyOneChild = { ... parent, path: '', noShowingChildren: true } return true } return false } }

动态组件的创建

有时候我们有很多类似的组件,只有一点点地方不一样,我们可以把这样的类似组件写到配置文件中,动态创建和引用组件

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/a93651d3517ca5d2423e2a328c8c0ad0.html