vue 面试题
基础
v-if和v-show的区别:
- v-if隐藏是 移除dom节点
v-show是隐藏css display:none - 频繁切换显示与隐藏建议用v-show , 次数少建议用v-if
watch与computed的区别:
- watch监听数据变化
computed 从现有数据计算新的数据 - watch没有缓存
computed有缓存 - watch 不能异步
computed 能异步 - watch 一对多关系
computed 多对一关系
v-for循环
key的作用:
是vue 需要dom进行patch(比较差异) 重要标识使用唯一的 key 值能有效的优化 vue dom模板渲染
key 建议使用数据唯一注意 id docid
避免使用 index 与随机
事件
- vue事件是原生js事件挂载到当前元素
- 事件修饰符
stop
once
prevent - 按键修饰符
.up
.enter
.esc
.space
.left.right.bottom.top
组件传参
- props 父传子
- $emit 子传父
- bus = new Vue()
跨层传递
this. o n ( n a m e . e = > ) t h i s . on(name.e=>{}) this. on(name.e=>)this.emit(name.data)
this.$off(name)
生命周期顺序
-
创建
由外到内创建,从内到外渲染
parent beforeCreated
son beforeCreated
son mounted
parent mounted -
更新
由外到内,从内到外
parent beforeUpdate
son beforeUpdate
son updated
parent updated