// vue3<script setup> import { onMounted } from ’vue’; // 使用前需引入生命周期钩子onMounted(() => { // …});// 可将不同的逻辑拆开成多个onMounted,依然按顺序执行,不会被覆盖onMounted(() => { // …});</script>// vue2<script> export default { mounted() { // 直接调用生命周期钩子 // … }, }</script>
讯享网
vue2vue3
beforeCreate
created
beforeMountonBeforeMountmountedonMountedbeforeUpdateonBeforeUpdateupdatedonUpdatedbeforeDestroyonBeforeUnmountdestroyedonUnmounted
created
beforeMountonBeforeMountmountedonMountedbeforeUpdateonBeforeUpdateupdatedonUpdatedbeforeDestroyonBeforeUnmountdestroyedonUnmounted
讯享网// vue2中在template里存在多个根节点会报错<template> <header></header> <main></main> <footer></footer></template>// 只能存在一个根节点,需要用一个<div>来包裹着<template> <div> <header></header> <main></main> <footer></footer> </div></template>
<template> <header></header> <main></main> <footer></footer></template>
更多vue面试题解答参见 前端vue面试题详细解答
讯享网<tempalte> <suspense> <template #default> <List /> </template> <template #fallback> <div> Loading… </div> </template> </suspense></template>
<button @click=“dialogVisible = true”>显示弹窗</button><teleport to=“div”> <div class=“dialog” v-if=“dialogVisible”> 我是弹窗,我直接移动到了div标签下 </div></teleport>
讯享网let obj = {};let name = ’leo’;Object.defineProperty(obj, ’name’, { enumerable: true, // 可枚举(是否可通过 for…in 或 Object.keys() 进行访问) configurable: true, // 可配置(是否可使用 delete 删除,是否可再次设置属性) // value: ”, // 任意类型的值,默认undefined // writable: true, // 可重写 get() { return name; }, set(value) { name = value; }});
function defineReactive(obj, key, val) { // 一 key 一个 dep const dep = new Dep() // 获取 key 的属性描述符,发现它是不可配置对象的话直接 return const property = Object.getOwnPropertyDescriptor(obj, key) if (property && property.configurable === false) { return } // 获取 getter 和 setter,并获取 val 值 const getter = property && property.get const setter = property && property.set if((!getter || setter) && arguments.length === 2) { val = obj[key] } // 递归处理,保证对象中所有 key 被观察 let childOb = observe(val) Object.defineProperty(obj, key, { enumerable: true, configurable: true, // get 劫持 obj[key] 的 进行依赖收集 get: function reactiveGetter() { const value = getter ? getter.call(obj) : val if(Dep.target) { // 依赖收集 dep.depend() if(childOb) { // 针对嵌套对象,依赖收集 childOb.dep.depend() // 触发数组响应式 if(Array.isArray(value)) { dependArray(value) } } } } return value }) // set 派发更新 obj[key] set: function reactiveSetter(newVal) { … if(setter) { setter.call(obj, newVal) } else { val = newVal } // 新值设置响应式 childOb = observe(val) // 依赖通知更新 dep.notify() }}
讯享网let handler = { get(obj, prop) { return prop in obj ? obj[prop] : ”; }, set() { // … }, …};
function createReactiveObject(target, isReadOnly, baseHandlers, collectionHandlers, proxyMap) { … // collectionHandlers: 处理Map、Set、WeakMap、WeakSet // baseHandlers: 处理数组、对象 const proxy = new Proxy( target, targetType === TargetType.COLLECTION ? collectionHandlers : baseHandlers ) proxyMap.set(target, proxy) return proxy}
讯享网<div id=“app”> <h1>vue3虚拟DOM讲解</h1> <p>今天天气真不错</p> <div>{{name}}</div></div>
import { createElementVNode as _createElementVNode, toDisplayString as _toDisplayString, openBlock as _openBlock, createElementBlock as _createElementBlock, pushScopeId as _pushScopeId, popScopeId as _popScopeId } from vueconst _withScopeId = n => (_pushScopeId(scope-id),n=n(),_popScopeId(),n)const _hoisted_1 = { id: app }const _hoisted_2 = /#PURE/ _withScopeId(() => /#PURE/_createElementVNode(h1, null, vue3虚拟DOM讲解, -1 / HOISTED /))const _hoisted_3 = /#PURE/ _withScopeId(() => /#PURE/_createElementVNode(p, null, 今天天气真不错, -1 / HOISTED /))export function render(_ctx, _cache, \(props, \)setup, \(data, \)options) { return (_openBlock(), _createElementBlock(div, _hoisted_1, [ _hoisted_2, _hoisted_3, _createElementVNode(div, null, _toDisplayString(_ctx.name), 1 / TEXT /) ]))}
讯享网// patchFlags 字段类型列举export const enum PatchFlags { TEXT = 1, // 动态文本内容 CLASS = 1 << 1, // 动态类名 STYLE = 1 << 2, // 动态样式 PROPS = 1 << 3, // 动态属性,不包含类名和样式 FULL_PROPS = 1 << 4, // 具有动态 key 属性,当 key 改变,需要进行完整的 diff 比较 HYDRATE_EVENTS = 1 << 5, // 带有监听事件的节点 STABLE_FRAGMENT = 1 << 6, // 不会改变子节点顺序的 fragment KEYED_FRAGMENT = 1 << 7, // 带有 key 属性的 fragment 或部分子节点 UNKEYED_FRAGMENT = 1 << 8, // 子节点没有 key 的fragment NEED_PATCH = 1 << 9, // 只会进行非 props 的比较 DYNAMIC_SLOTS = 1 << 10, // 动态的插槽 HOISTED = -1, // 静态节点,diff阶段忽略其子节点 BAIL = -2 // 代表 diff 应该结束}
<div id=“app”> <h1>vue3事件缓存讲解</h1> <p>今天天气真不错</p> <div>{{name}}</div> <span onCLick=() => {}><span></div>
讯享网import { createElementVNode as _createElementVNode, toDisplayString as _toDisplayString, openBlock as _openBlock, createElementBlock as _createElementBlock, pushScopeId as _pushScopeId, popScopeId as _popScopeId } from vueconst _withScopeId = n => (_pushScopeId(scope-id),n=n(),_popScopeId(),n)const _hoisted_1 = { id: app }const _hoisted_2 = /#PURE/ _withScopeId(() => /#PURE/_createElementVNode(h1, null, vue3事件缓存讲解, -1 / HOISTED /))const _hoisted_3 = /#PURE/ _withScopeId(() => /#PURE/_createElementVNode(p, null, 今天天气真不错, -1 / HOISTED /))const _hoisted_4 = /#PURE/ _withScopeId(() => /#PURE/_createElementVNode(span, { onCLick: () => {} }, [ /#PURE/_createElementVNode(span)], -1 / HOISTED /))export function render(_ctx, _cache, \(props, \)setup, \(data, \)options) { return (_openBlock(), _createElementBlock(div, _hoisted_1, [ _hoisted_2, _hoisted_3, _createElementVNode(div, null, _toDisplayString(_ctx.name), 1 / TEXT /), _hoisted_4 ]))}
function patchChildren(n1, n2, container, parentAnchor, parentComponent, parentSuspense, isSVG, optimized) { // 获取新老孩子节点 const c1 = n1 && n1.children const c2 = n2.children const prevShapeFlag = n1 ? n1.shapeFlag : 0 const { patchFlag, shapeFlag } = n2 // 处理 patchFlag 大于 0 if(patchFlag > 0) { if(patchFlag && PatchFlags.KEYED_FRAGMENT) { // 存在 key patchKeyedChildren() return } els if(patchFlag && PatchFlags.UNKEYED_FRAGMENT) { // 不存在 key patchUnkeyedChildren() return } } // 匹配是文本节点(静态):移除老节点,设置文本节点 if(shapeFlag && ShapeFlags.TEXT_CHILDREN) { if (prevShapeFlag & ShapeFlags.ARRAY_CHILDREN) { unmountChildren(c1 as VNode[], parentComponent, parentSuspense) } if (c2 !== c1) { hostSetElementText(container, c2 as string) } } else { // 匹配新老 Vnode 是数组,则全量比较;否则移除当前所有的节点 if (prevShapeFlag & ShapeFlags.ARRAY_CHILDREN) { if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) { patchKeyedChildren(c1, c2, container, anchor, parentComponent, parentSuspense,…) } else { unmountChildren(c1 as VNode[], parentComponent, parentSuspense, true) } } else { if(prevShapeFlag & ShapeFlags.TEXT_CHILDREN) { hostSetElementText(container, ”) } if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) { mountChildren(c2 as VNodeArrayChildren, container,anchor,parentComponent,…) } } }}
讯享网function patchUnkeyedChildren(c1, c2, container, parentAnchor, parentComponent, parentSuspense, isSVG, optimized) { c1 = c1 || EMPTY_ARR c2 = c2 || EMPTY_ARR const oldLength = c1.length const newLength = c2.length const commonLength = Math.min(oldLength, newLength) let i for(i = 0; i < commonLength; i++) { // 如果新 Vnode 已经挂载,则直接 clone 一份,否则新建一个节点 const nextChild = (c2[i] = optimized ? cloneIfMounted(c2[i] as Vnode)) : normalizeVnode(c2[i]) patch() } if(oldLength > newLength) { // 移除多余的节点 unmountedChildren() } else { // 创建新的节点 mountChildren() }}
import Vue from ’vue’;Vue.nextTick(() => { // 一些和DOM有关的东西});
讯享网import { nextTick } from ’vue’; // 显式导入nextTick(() => { // 一些和DOM有关的东西});
<template> <button @click=“increment”>count is: {{ count }}</button></template><script>export default { data() { return { count: 0 } }, methods: { increment() { this.count++; } }, mounted() { console.log(The initial count is ${this.count}.); }}</script>
讯享网

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/143706.html