共计 81992 个字符,预计需要花费 205 分钟才能阅读完成。
computed 的实现原理
computed 实质是一个惰性求值的观察者。
computed 外部实现了一个惰性的 watcher, 也就是 computed watcher,computed watcher 不会立即求值, 同时持有一个 dep 实例。
其外部通过 this.dirty 属性标记计算属性是否须要从新求值。
当 computed 的依赖状态产生扭转时, 就会告诉这个惰性的 watcher,
computed watcher 通过 this.dep.subs.length 判断有没有订阅者,
有的话, 会从新计算, 而后比照新旧值, 如果变动了, 会从新渲染。(Vue 想确保不仅仅是计算属性依赖的值发生变化,而是当计算属性最终计算的值发生变化时才会触发渲染 watcher 从新渲染,实质上是一种优化。)
没有的话, 仅仅把 this.dirty = true。(当计算属性依赖于其余数据时,属性并不会立刻从新计算,只有之后其余中央须要读取属性的时候,它才会真正计算,即具备 lazy(懒计算)个性。)
Vue-router 跳转和 location.href 有什么区别
- 应用
location.href= /url
来跳转,简略不便,然而刷新了页面; - 应用
history.pushState(/url)
,无刷新页面,动态跳转; - 引进 router,而后应用
router.push(/url)
来跳转,应用了diff
算法,实现了按需加载,缩小了 dom 的耗费。其实应用 router 跳转和应用history.pushState()
没什么差异的,因为 vue-router 就是用了history.pushState()
,尤其是在 history 模式下。
$nextTick 原理及作用
Vue 的 nextTick 其本质是对 JavaScript 执行原理 EventLoop 的一种利用。
nextTick 的外围是利用了如 Promise、MutationObserver、setImmediate、setTimeout 的原生 JavaScript 办法来模仿对应的微 / 宏工作的实现,实质是为了利用 JavaScript 的这些异步回调工作队列来实现 Vue 框架中本人的异步回调队列。
nextTick 不仅是 Vue 外部的异步队列的调用办法,同时也容许开发者在理论我的项目中应用这个办法来满足理论利用中对 DOM 更新数据机会的后续逻辑解决
nextTick 是典型的将底层 JavaScript 执行原理利用到具体案例中的示例,引入异步更新队列机制的起因∶
- 如果是同步更新,则屡次对一个或多个属性赋值,会频繁触发 UI/DOM 的渲染,能够缩小一些无用渲染
- 同时因为 VirtualDOM 的引入,每一次状态发生变化后,状态变动的信号会发送给组件,组件外部应用 VirtualDOM 进行计算得出须要更新的具体的 DOM 节点,而后对 DOM 进行更新操作,每次更新状态后的渲染过程须要更多的计算,而这种无用功也将节约更多的性能,所以异步渲染变得更加至关重要
Vue 采纳了数据驱动视图的思维,然而在一些状况下,依然须要操作 DOM。有时候,可能遇到这样的状况,DOM1 的数据产生了变动,而 DOM2 须要从 DOM1 中获取数据,那这时就会发现 DOM2 的视图并没有更新,这时就须要用到了 nextTick
了。
因为 Vue 的 DOM 操作是异步的,所以,在下面的状况中,就要将 DOM2 获取数据的操作写在 $nextTick
中。
this.$nextTick(() => { // 获取数据的操作...})
所以,在以下状况下,会用到 nextTick:
- 在数据变动后执行的某个操作,而这个操作须要应用随数据变动而变动的 DOM 构造的时候,这个操作就须要办法在
nextTick()
的回调函数中。 - 在 vue 生命周期中,如果在 created()钩子进行 DOM 操作,也肯定要放在
nextTick()
的回调函数中。
因为在 created()钩子函数中,页面的 DOM 还未渲染,这时候也没方法操作 DOM,所以,此时如果想要操作 DOM,必须将操作的代码放在 nextTick()
的回调函数中。
Vue3.0 和 2.0 的响应式原理区别
Vue3.x 改用 Proxy 代替 Object.defineProperty。因为 Proxy 能够间接监听对象和数组的变动,并且有多达 13 种拦挡办法。
相干代码如下
import {mutableHandlers} from "./baseHandlers"; // 代理相干逻辑 | |
import {isObject} from "./util"; // 工具办法 | |
export function reactive(target) { | |
// 依据不同参数创立不同响应式对象 | |
return createReactiveObject(target, mutableHandlers); | |
} | |
function createReactiveObject(target, baseHandler) {if (!isObject(target)) {return target;} | |
const observed = new Proxy(target, baseHandler); | |
return observed; | |
} | |
const get = createGetter(); | |
const set = createSetter(); | |
function createGetter() {return function get(target, key, receiver) { | |
// 对获取的值进行喷射 | |
const res = Reflect.get(target, key, receiver); | |
console.log("属性获取", key); | |
if (isObject(res)) { | |
// 如果获取的值是对象类型,则返回以后对象的代理对象 | |
return reactive(res); | |
} | |
return res; | |
}; | |
} | |
function createSetter() {return function set(target, key, value, receiver) {const oldValue = target[key]; | |
const hadKey = hasOwn(target, key); | |
const result = Reflect.set(target, key, value, receiver); | |
if (!hadKey) {console.log("属性新增", key, value); | |
} else if (hasChanged(value, oldValue)) {console.log("属性值被批改", key, value); | |
} | |
return result; | |
}; | |
} | |
export const mutableHandlers = { | |
get, // 当获取属性时调用此办法 | |
set, // 当批改属性时调用此办法 | |
}; |
vue3 中 watch、watchEffect 区别
watch
是惰性执行,也就是只有监听的值发生变化的时候才会执行,然而watchEffect
不同,每次代码加载watchEffect
都会执行(疏忽watch
第三个参数的配置,如果批改配置项也能够实现立刻执行)watch
须要传递监听的对象,watchEffect
不须要watch
只能监听响应式数据:ref
定义的属性和reactive
定义的对象,如果间接监听reactive
定义对象中的属性是不容许的(会报正告),除非应用函数转换一下。其实就是官网上说的监听一个getter
watchEffect
如果监听reactive
定义的对象是不起作用的,只能监听对象中的属性
看一下 watchEffect
的代码
<template> | |
<div> | |
请输出 firstName:<input type="text" v-model="firstName"> | |
</div> | |
<div> | |
请输出 lastName:<input type="text" v-model="lastName"> | |
</div> | |
<div> | |
请输出 obj.text:<input type="text" v-model="obj.text"> | |
</div> | |
<div>【obj.text】{{obj.text}} | |
</div> | |
</template> | |
<script> | |
import {ref, reactive, watch, watchEffect} from 'vue' | |
export default { | |
name: "HelloWorld", | |
props: {msg: String,}, | |
setup(props,content){let firstName = ref('') | |
let lastName = ref('') | |
let obj= reactive({text:'hello'}) | |
watchEffect(()=>{console.log('触发了 watchEffect'); | |
console.log(` 组合后的名称为:${firstName.value}${lastName.value}`) | |
}) | |
return{ | |
obj, | |
firstName, | |
lastName | |
} | |
} | |
}; | |
</script> |
革新一下代码
watchEffect(()=>{console.log('触发了 watchEffect'); | |
// 这里咱们不应用 firstName.value/lastName.value,相当于是监控整个 ref, 对应第四点下面的论断 | |
console.log(` 组合后的名称为:${firstName}${lastName}`) | |
}) |
watchEffect(()=>{console.log('触发了 watchEffect'); | |
console.log(obj); | |
}) |
略微革新一下
let obj = reactive({text:'hello'}) | |
watchEffect(()=>{console.log('触发了 watchEffect'); | |
console.log(obj.text); | |
}) |
再看一下 watch 的代码,验证一下
let obj= reactive({text:'hello'}) | |
// watch 是惰性执行,默认初始化之后不会执行,只有值有变动才会触发,可通过配置参数实现默认执行 | |
watch(obj, (newValue, oldValue) => { | |
// 回调函数 | |
console.log('触发监控更新了 new', newValue); | |
console.log('触发监控更新了 old', oldValue); | |
},{ | |
// 配置 immediate 参数,立刻执行,以及深层次监听 | |
immediate: true, | |
deep: true | |
}) |
- 监控整个
reactive
对象,从下面的图能够看到deep
理论默认是开启的,就算咱们设置为false
也还是有效。而且旧值获取不到。 - 要获取旧值则须要监控对象的属性,也就是监听一个
getter
,看下图
总结
- 如果定义了
reactive
的数据,想去应用watch
监听数据扭转,则无奈正确获取旧值,并且deep
属性配置有效,主动强制开启了深层次监听。 - 如果应用
ref
初始化一个对象或者数组类型的数据,会被主动转成reactive
的实现形式,生成proxy
代理对象。也会变得无奈正确取旧值。 - 用任何形式生成的数据,如果接管的变量是一个
proxy
代理对象,就都会导致watch
这个对象时,watch
回调里无奈正确获取旧值。 - 所以当大家应用
watch
监听对象时,如果在不须要应用旧值的状况,能够失常监听对象没关系;然而如果当监听扭转函数外面须要用到旧值时,只能监听 对象.xxx` 属性 的形式才行
watch 和 watchEffect 异同总结
体验
watchEffect
立刻运行一个函数,而后被动地追踪它的依赖,当这些依赖扭转时从新执行该函数
const count = ref(0) | |
| |
watchEffect(() => console.log(count.value)) | |
// -> logs 0 | |
| |
count.value++ | |
// -> logs 1 |
watch
侦测一个或多个响应式数据源并在数据源变动时调用一个回调函数
const state = reactive({count: 0}) | |
watch(() => state.count, | |
(count, prevCount) => {/* ... */} | |
) |
答复范例
watchEffect
立刻运行一个函数,而后被动地追踪它的依赖,当这些依赖扭转时从新执行该函数。watch
侦测一个或多个响应式数据源并在数据源变动时调用一个回调函数watchEffect(effect)
是一种非凡watch
,传入的函数既是依赖收集的数据源,也是回调函数。如果咱们不关怀响应式数据变动前后的值,只是想拿这些数据做些事件,那么watchEffect
就是咱们须要的。watch
更底层,能够接管多种数据源,包含用于依赖收集的getter
函数,因而它齐全能够实现watchEffect
的性能,同时因为能够指定getter
函数,依赖能够管制的更准确,还能获取数据变动前后的值,因而如果须要这些时咱们会应用watch
watchEffect
在应用时,传入的函数会立即执行一次。watch
默认状况下并不会执行回调函数,除非咱们手动设置immediate
选项- 从实现上来说,
watchEffect(fn)
相当于watch(fn,fn,{immediate:true})
watchEffect
定义如下
export function watchEffect( | |
effect: WatchEffect, | |
options?: WatchOptionsBase | |
): WatchStopHandle {return doWatch(effect, null, options) | |
} |
watch
定义如下
export function watch<T = any, Immediate extends Readonly<boolean> = false>( | |
source: T | WatchSource<T>, | |
cb: any, | |
options?: WatchOptions<Immediate> | |
): WatchStopHandle {return doWatch(source as any, cb, options) | |
} |
很显著 watchEffect
就是一种非凡的 watch
实现。
nextTick 应用场景和原理
nextTick 中的回调是在下次 DOM 更新循环完结之后执行的提早回调。在批改数据之后立刻应用这个办法,获取更新后的 DOM。次要思路就是采纳微工作优先的形式调用异步办法去执行 nextTick 包装的办法
相干代码如下
let callbacks = []; | |
let pending = false; | |
function flushCallbacks() { | |
pending = false; // 把标记还原为 false | |
// 顺次执行回调 | |
for (let i = 0; i < callbacks.length; i++) {callbacks[i]();} | |
} | |
let timerFunc; // 定义异步办法 采纳优雅降级 | |
if (typeof Promise !== "undefined") { | |
// 如果反对 promise | |
const p = Promise.resolve(); | |
timerFunc = () => {p.then(flushCallbacks); | |
}; | |
} else if (typeof MutationObserver !== "undefined") { | |
// MutationObserver 次要是监听 dom 变动 也是一个异步办法 | |
let counter = 1; | |
const observer = new MutationObserver(flushCallbacks); | |
const textNode = document.createTextNode(String(counter)); | |
observer.observe(textNode, {characterData: true,}); | |
timerFunc = () => {counter = (counter + 1) % 2; | |
textNode.data = String(counter); | |
}; | |
} else if (typeof setImmediate !== "undefined") { | |
// 如果后面都不反对 判断 setImmediate | |
timerFunc = () => {setImmediate(flushCallbacks); | |
}; | |
} else { | |
// 最初降级采纳 setTimeout | |
timerFunc = () => {setTimeout(flushCallbacks, 0); | |
}; | |
} | |
export function nextTick(cb) { | |
// 除了渲染 watcher 还有用户本人手动调用的 nextTick 一起被收集到数组 | |
callbacks.push(cb); | |
if (!pending) { | |
// 如果屡次调用 nextTick 只会执行一次异步 等异步队列清空之后再把标记变为 false | |
pending = true; | |
timerFunc();} | |
} |
vue2.x 具体
1. 剖析
首先找到 vue
的构造函数
源码地位:src\core\instance\index.js
function Vue (options) { | |
if (process.env.NODE_ENV !== 'production' && | |
!(this instanceof Vue) | |
) {warn('Vue is a constructor and should be called with the `new` keyword') | |
} | |
this._init(options) | |
} |
options
是用户传递过去的配置项,如 data、methods
等罕用的办法
vue
构建函数调用 _init
办法,但咱们发现本文件中并没有此办法,但认真能够看到文件下方定定义了很多初始化办法
initMixin(Vue); // 定义 _init | |
stateMixin(Vue); // 定义 $set $get $delete $watch 等 | |
eventsMixin(Vue); // 定义事件 $on $once $off $emit | |
lifecycleMixin(Vue);// 定义 _update $forceUpdate $destroy | |
renderMixin(Vue); // 定义 _render 返回虚构 dom |
首先能够看 initMixin
办法,发现该办法在 Vue
原型上定义了 _init
办法
源码地位:src\core\instance\init.js
Vue.prototype._init = function (options?: Object) { | |
const vm: Component = this | |
// a uid | |
vm._uid = uid++ | |
let startTag, endTag | |
/* istanbul ignore if */ | |
if (process.env.NODE_ENV !== 'production' && config.performance && mark) {startTag = `vue-perf-start:${vm._uid}` | |
endTag = `vue-perf-end:${vm._uid}` | |
mark(startTag) | |
} | |
// a flag to avoid this being observed | |
vm._isVue = true | |
// merge options | |
// 合并属性,判断初始化的是否是组件,这里合并次要是 mixins 或 extends 的办法 | |
if (options && options._isComponent) { | |
// optimize internal component instantiation | |
// since dynamic options merging is pretty slow, and none of the | |
// internal component options needs special treatment. | |
initInternalComponent(vm, options) | |
} else { // 合并 vue 属性 | |
vm.$options = mergeOptions(resolveConstructorOptions(vm.constructor), | |
options || {}, | |
vm | |
) | |
} | |
/* istanbul ignore else */ | |
if (process.env.NODE_ENV !== 'production') { | |
// 初始化 proxy 拦截器 | |
initProxy(vm) | |
} else {vm._renderProxy = vm} | |
// expose real self | |
vm._self = vm | |
// 初始化组件生命周期标记位 | |
initLifecycle(vm) | |
// 初始化组件事件侦听 | |
initEvents(vm) | |
// 初始化渲染办法 | |
initRender(vm) | |
callHook(vm, 'beforeCreate') | |
// 初始化依赖注入内容,在初始化 data、props 之前 | |
initInjections(vm) // resolve injections before data/props | |
// 初始化 props/data/method/watch/methods | |
initState(vm) | |
initProvide(vm) // resolve provide after data/props | |
callHook(vm, 'created') | |
/* istanbul ignore if */ | |
if (process.env.NODE_ENV !== 'production' && config.performance && mark) {vm._name = formatComponentName(vm, false) | |
mark(endTag) | |
measure(`vue ${vm._name} init`, startTag, endTag) | |
} | |
// 挂载元素 | |
if (vm.$options.el) {vm.$mount(vm.$options.el) | |
} | |
} |
仔细阅读下面的代码,咱们失去以下论断:
- 在调用
beforeCreate
之前,数据初始化并未实现,像data
、props
这些属性无法访问到 - 到了
created
的时候,数据曾经初始化实现,可能拜访data
、props
这些属性,但这时候并未实现dom
的挂载,因而无法访问到dom
元素 - 挂载办法是调用
vm.$mount
办法
initState
办法是实现 props/data/method/watch/methods
的初始化
源码地位:src\core\instance\state.js
export function initState (vm: Component) { | |
// 初始化组件的 watcher 列表 | |
vm._watchers = [] | |
const opts = vm.$options | |
// 初始化 props | |
if (opts.props) initProps(vm, opts.props) | |
// 初始化 methods 办法 | |
if (opts.methods) initMethods(vm, opts.methods) | |
if (opts.data) { | |
// 初始化 data | |
initData(vm) | |
} else {observe(vm._data = {}, true /* asRootData */) | |
} | |
if (opts.computed) initComputed(vm, opts.computed) | |
if (opts.watch && opts.watch !== nativeWatch) {initWatch(vm, opts.watch) | |
} | |
} |
咱们和这里次要看初始化 data
的办法为 initData
,它与initState
在同一文件上
function initData (vm: Component) { | |
let data = vm.$options.data | |
// 获取到组件上的 data | |
data = vm._data = typeof data === 'function' | |
? getData(data, vm) | |
: data || {} | |
if (!isPlainObject(data)) {data = {} | |
process.env.NODE_ENV !== 'production' && warn( | |
'data functions should return an object:\n' + | |
'https://vuejs.org/v2/guide/components.html#data-Must-Be-a-Function', | |
vm | |
) | |
} | |
// proxy data on instance | |
const keys = Object.keys(data) | |
const props = vm.$options.props | |
const methods = vm.$options.methods | |
let i = keys.length | |
while (i--) {const key = keys[i] | |
if (process.env.NODE_ENV !== 'production') { | |
// 属性名不能与办法名反复 | |
if (methods && hasOwn(methods, key)) { | |
warn(`Method "${key}" has already been defined as a data property.`, | |
vm | |
) | |
} | |
} | |
// 属性名不能与 state 名称反复 | |
if (props && hasOwn(props, key)) { | |
process.env.NODE_ENV !== 'production' && warn(`The data property "${key}" is already declared as a prop. ` + | |
`Use prop default value instead.`, | |
vm | |
) | |
} else if (!isReserved(key)) { // 验证 key 值的合法性 | |
// 将_data 中的数据挂载到组件 vm 上, 这样就能够通过 this.xxx 拜访到组件上的数据 | |
proxy(vm, `_data`, key) | |
} | |
} | |
// observe data | |
// 响应式监听 data 是数据的变动 | |
observe(data, true /* asRootData */) | |
} |
仔细阅读下面的代码,咱们能够失去以下论断:
- 初始化程序:
props
、methods
、data
data
定义的时候可选择函数模式或者对象模式(组件只能为函数模式)
对于数据响应式在这就不开展具体阐明
上文提到挂载办法是调用 vm.$mount
办法
源码地位:
Vue.prototype.$mount = function ( | |
el?: string | Element, | |
hydrating?: boolean | |
): Component { | |
// 获取或查问元素 | |
el = el && query(el) | |
/* istanbul ignore if */ | |
// vue 不容许间接挂载到 body 或页面文档上 | |
if (el === document.body || el === document.documentElement) { | |
process.env.NODE_ENV !== 'production' && warn(`Do not mount Vue to <html> or <body> - mount to normal elements instead.`) | |
return this | |
} | |
const options = this.$options | |
// resolve template/el and convert to render function | |
if (!options.render) { | |
let template = options.template | |
// 存在 template 模板,解析 vue 模板文件 | |
if (template) {if (typeof template === 'string') {if (template.charAt(0) === '#') {template = idToTemplate(template) | |
/* istanbul ignore if */ | |
if (process.env.NODE_ENV !== 'production' && !template) { | |
warn(`Template element not found or is empty: ${options.template}`, | |
this | |
) | |
} | |
} | |
} else if (template.nodeType) {template = template.innerHTML} else {if (process.env.NODE_ENV !== 'production') {warn('invalid template option:' + template, this) | |
} | |
return this | |
} | |
} else if (el) { | |
// 通过选择器获取元素内容 | |
template = getOuterHTML(el) | |
} | |
if (template) { | |
/* istanbul ignore if */ | |
if (process.env.NODE_ENV !== 'production' && config.performance && mark) {mark('compile') | |
} | |
/** | |
* 1. 将 temmplate 解析 ast tree | |
* 2. 将 ast tree 转换成 render 语法字符串 | |
* 3. 生成 render 办法 | |
*/ | |
const {render, staticRenderFns} = compileToFunctions(template, { | |
outputSourceRange: process.env.NODE_ENV !== 'production', | |
shouldDecodeNewlines, | |
shouldDecodeNewlinesForHref, | |
delimiters: options.delimiters, | |
comments: options.comments | |
}, this) | |
options.render = render | |
options.staticRenderFns = staticRenderFns | |
/* istanbul ignore if */ | |
if (process.env.NODE_ENV !== 'production' && config.performance && mark) {mark('compile end') | |
measure(`vue ${this._name} compile`, 'compile', 'compile end') | |
} | |
} | |
} | |
return mount.call(this, el, hydrating) | |
} |
浏览下面代码,咱们能失去以下论断:
- 不要将根元素放到
body
或者html
上 - 能够在对象中定义
template/render
或者间接应用template
、el
示意元素选择器 - 最终都会解析成
render
函数,调用compileToFunctions
,会将template
解析成render
函数
对 template
的解析步骤大抵分为以下几步:
- 将
html
文档片段解析成ast
描述符 - 将
ast
描述符解析成字符串 - 生成
render
函数
生成 render
函数,挂载到 vm
上后,会再次调用 mount
办法
源码地位:src\platforms\web\runtime\index.js
// public mount method | |
Vue.prototype.$mount = function ( | |
el?: string | Element, | |
hydrating?: boolean | |
): Component {el = el && inBrowser ? query(el) : undefined | |
// 渲染组件 | |
return mountComponent(this, el, hydrating) | |
} |
调用 mountComponent
渲染组件
export function mountComponent ( | |
vm: Component, | |
el: ?Element, | |
hydrating?: boolean | |
): Component { | |
vm.$el = el | |
// 如果没有获取解析的 render 函数,则会抛出正告 | |
// render 是解析模板文件生成的 | |
if (!vm.$options.render) { | |
vm.$options.render = createEmptyVNode | |
if (process.env.NODE_ENV !== 'production') { | |
/* istanbul ignore if */ | |
if ((vm.$options.template && vm.$options.template.charAt(0) !== '#') || | |
vm.$options.el || el) { | |
warn( | |
'You are using the runtime-only build of Vue where the template' + | |
'compiler is not available. Either pre-compile the templates into' + | |
'render functions, or use the compiler-included build.', | |
vm | |
) | |
} else { | |
// 没有获取到 vue 的模板文件 | |
warn( | |
'Failed to mount component: template or render function not defined.', | |
vm | |
) | |
} | |
} | |
} | |
// 执行 beforeMount 钩子 | |
callHook(vm, 'beforeMount') | |
let updateComponent | |
/* istanbul ignore if */ | |
if (process.env.NODE_ENV !== 'production' && config.performance && mark) {updateComponent = () => { | |
const name = vm._name | |
const id = vm._uid | |
const startTag = `vue-perf-start:${id}` | |
const endTag = `vue-perf-end:${id}` | |
mark(startTag) | |
const vnode = vm._render() | |
mark(endTag) | |
measure(`vue ${name} render`, startTag, endTag) | |
mark(startTag) | |
vm._update(vnode, hydrating) | |
mark(endTag) | |
measure(`vue ${name} patch`, startTag, endTag) | |
} | |
} else { | |
// 定义更新函数 | |
updateComponent = () => { | |
// 理论调⽤是在 lifeCycleMixin 中定义的_update 和 renderMixin 中定义的_render | |
vm._update(vm._render(), hydrating) | |
} | |
} | |
// we set this to vm._watcher inside the watcher's constructor | |
// since the watcher's initial patch may call $forceUpdate (e.g. inside child | |
// component's mounted hook), which relies on vm._watcher being already defined | |
// 监听以后组件状态,当有数据变动时,更新组件 | |
new Watcher(vm, updateComponent, noop, {before () {if (vm._isMounted && !vm._isDestroyed) { | |
// 数据更新引发的组件更新 | |
callHook(vm, 'beforeUpdate') | |
} | |
} | |
}, true /* isRenderWatcher */) | |
hydrating = false | |
// manually mounted instance, call mounted on self | |
// mounted is called for render-created child components in its inserted hook | |
if (vm.$vnode == null) { | |
vm._isMounted = true | |
callHook(vm, 'mounted') | |
} | |
return vm | |
} |
浏览下面代码,咱们失去以下论断:
- 会触发
boforeCreate
钩子 - 定义
updateComponent
渲染页面视图的办法 - 监听组件数据,一旦发生变化,触发
beforeUpdate
生命钩子
updateComponent
办法次要执行在 vue
初始化时申明的 render
,update
办法
render的作用次要是生成
vnode
源码地位:src\core\instance\render.js
// 定义 vue 原型上的 render 办法 | |
Vue.prototype._render = function (): VNode { | |
const vm: Component = this | |
// render 函数来自于组件的 option | |
const {render, _parentVnode} = vm.$options | |
if (_parentVnode) { | |
vm.$scopedSlots = normalizeScopedSlots( | |
_parentVnode.data.scopedSlots, | |
vm.$slots, | |
vm.$scopedSlots | |
) | |
} | |
// set parent vnode. this allows render functions to have access | |
// to the data on the placeholder node. | |
vm.$vnode = _parentVnode | |
// render self | |
let vnode | |
try { | |
// There's no need to maintain a stack because all render fns are called | |
// separately from one another. Nested component's render fns are called | |
// when parent component is patched. | |
currentRenderingInstance = vm | |
// 调用 render 办法,本人的独特的 render 办法,传入 createElement 参数,生成 vNode | |
vnode = render.call(vm._renderProxy, vm.$createElement) | |
} catch (e) {handleError(e, vm, `render`) | |
// return error render result, | |
// or previous vnode to prevent render error causing blank component | |
/* istanbul ignore else */ | |
if (process.env.NODE_ENV !== 'production' && vm.$options.renderError) { | |
try {vnode = vm.$options.renderError.call(vm._renderProxy, vm.$createElement, e) | |
} catch (e) {handleError(e, vm, `renderError`) | |
vnode = vm._vnode | |
} | |
} else {vnode = vm._vnode} | |
} finally {currentRenderingInstance = null} | |
// if the returned array contains only a single node, allow it | |
if (Array.isArray(vnode) && vnode.length === 1) {vnode = vnode[0] | |
} | |
// return empty vnode in case the render function errored out | |
if (!(vnode instanceof VNode)) {if (process.env.NODE_ENV !== 'production' && Array.isArray(vnode)) { | |
warn( | |
'Multiple root nodes returned from render function. Render function' + | |
'should return a single root node.', | |
vm | |
) | |
} | |
vnode = createEmptyVNode()} | |
// set parent | |
vnode.parent = _parentVnode | |
return vnode | |
} |
_update
次要性能是调用 patch
,将vnode
转换为实在DOM
,并且更新到页面中
源码地位:src\core\instance\lifecycle.js
Vue.prototype._update = function (vnode: VNode, hydrating?: boolean) { | |
const vm: Component = this | |
const prevEl = vm.$el | |
const prevVnode = vm._vnode | |
// 设置以后激活的作用域 | |
const restoreActiveInstance = setActiveInstance(vm) | |
vm._vnode = vnode | |
// Vue.prototype.__patch__ is injected in entry points | |
// based on the rendering backend used. | |
if (!prevVnode) { | |
// initial render | |
// 执行具体的挂载逻辑 | |
vm.$el = vm.__patch__(vm.$el, vnode, hydrating, false /* removeOnly */) | |
} else { | |
// updates | |
vm.$el = vm.__patch__(prevVnode, vnode) | |
} | |
restoreActiveInstance() | |
// update __vue__ reference | |
if (prevEl) {prevEl.__vue__ = null} | |
if (vm.$el) {vm.$el.__vue__ = vm} | |
// if parent is an HOC, update its $el as well | |
if (vm.$vnode && vm.$parent && vm.$vnode === vm.$parent._vnode) {vm.$parent.$el = vm.$el} | |
// updated hook is called by the scheduler to ensure that children are | |
// updated in a parent's updated hook. | |
} |
2. 论断
-
new Vue
的时候调用会调用_init
办法- 定义
$set
、$get
、$delete
、$watch
等办法 - 定义
$on
、$off
、$emit
、$off
等事件 - 定义
_update
、$forceUpdate
、$destroy
生命周期
- 定义
- 调用
$mount
进行页面的挂载 - 挂载的时候次要是通过
mountComponent
办法 - 定义
updateComponent
更新函数 - 执行
render
生成虚构DOM
_update
将虚构DOM
生成实在DOM
构造,并且渲染到页面中
created 和 mounted 的区别
- created: 在模板渲染成 html 前调用,即通常初始化某些属性值,而后再渲染成视图。
- mounted: 在模板渲染成 html 后调用,通常是初始化页面实现后,再对 html 的 dom 节点进行一些须要的操作。
Vue 中封装的数组办法有哪些,其如何实现页面更新
在 Vue 中,对响应式解决利用的是 Object.defineProperty 对数据进行拦挡,而这个办法并不能监听到数组外部变动,数组长度变动,数组的截取变动等,所以须要对这些操作进行 hack,让 Vue 能监听到其中的变动。那 Vue 是如何实现让这些数组办法实现元素的实时更新的呢,上面是 Vue 中对这些办法的封装:
// 缓存数组原型 | |
const arrayProto = Array.prototype; | |
// 实现 arrayMethods.__proto__ === Array.prototype | |
export const arrayMethods = Object.create(arrayProto); | |
// 须要进行性能拓展的办法 | |
const methodsToPatch = [ | |
"push", | |
"pop", | |
"shift", | |
"unshift", | |
"splice", | |
"sort", | |
"reverse" | |
]; | |
/** * Intercept mutating methods and emit events */ | |
methodsToPatch.forEach(function(method) { | |
// 缓存原生数组办法 | |
const original = arrayProto[method]; | |
def(arrayMethods, method, function mutator(...args) { | |
// 执行并缓存原生数组性能 | |
const result = original.apply(this, args); | |
// 响应式解决 | |
const ob = this.__ob__; | |
let inserted; | |
switch (method) { | |
// push、unshift 会新增索引,所以要手动 observer | |
case "push": | |
case "unshift": | |
inserted = args; | |
break; | |
// splice 办法,如果传入了第三个参数,也会有索引退出,也要手动 observer。case "splice": | |
inserted = args.slice(2); | |
break; | |
} | |
// | |
if (inserted) ob.observeArray(inserted);// 获取插入的值,并设置响应式监听 | |
// notify change | |
ob.dep.notify();// 告诉依赖更新 | |
// 返回原生数组办法的执行后果 | |
return result; | |
}); | |
}); |
简略来说就是,重写了数组中的那些原生办法,首先获取到这个数组的__ob__,也就是它的 Observer 对象,如果有新的值,就调用 observeArray 持续对新的值察看变动(也就是通过 target__proto__ == arrayMethods
来扭转了数组实例的型),而后手动调用 notify,告诉渲染 watcher,执行 update。
参考:前端 vue 面试题具体解答
vue 如何监听对象或者数组某个属性的变动
当在我的项目中间接设置数组的某一项的值,或者间接设置对象的某个属性值,这个时候,你会发现页面并没有更新。这是因为 Object.defineProperty()限度,监听不到变动。
解决形式:
- this.$set(你要扭转的数组 / 对象,你要扭转的地位 /key,你要改成什么 value)
this.$set(this.arr, 0, "OBKoro1"); // 扭转数组 this.$set(this.obj, "c", "OBKoro1"); // 扭转对象
- 调用以下几个数组的办法
splice()、push()、pop()、shift()、unshift()、sort()、reverse()
vue 源码里缓存了 array 的原型链,而后重写了这几个办法,触发这几个办法的时候会 observer 数据,意思是应用这些办法不必再进行额定的操作,视图主动进行更新。举荐应用 splice 办法会比拟好自定义, 因为 splice 能够在数组的任何地位进行删除 / 增加操作
vm.$set
的实现原理是:
- 如果指标是数组,间接应用数组的 splice 办法触发相应式;
- 如果指标是对象,会先判读属性是否存在、对象是否是响应式,最终如果要对属性进行响应式解决,则是通过调用 defineReactive 办法进行响应式解决(defineReactive 办法就是 Vue 在初始化对象时,给对象属性采纳 Object.defineProperty 动静增加 getter 和 setter 的性能所调用的办法)
Vue template 到 render 的过程
vue 的模版编译过程次要如下:template -> ast -> render 函数
vue 在模版编译版本的码中会执行 compileToFunctions 将 template 转化为 render 函数:
// 将模板编译为 render 函数 const {render, staticRenderFns} = compileToFunctions(template,options// 省略}, this)
CompileToFunctions 中的次要逻辑如下∶ (1)调用 parse 办法将 template 转化为 ast(形象语法树)
constast = parse(template.trim(), options)
- parse 的指标:把 tamplate 转换为 AST 树,它是一种用 JavaScript 对象的模式来形容整个模板。
- 解析过程:利用正则表达式程序解析模板,当解析到开始标签、闭合标签、文本的时候都会别离执行对应的 回调函数,来达到结构 AST 树的目标。
AST 元素节点总共三种类型:type 为 1 示意一般元素、2 为表达式、3 为纯文本
(2)对动态节点做优化
optimize(ast,options)
这个过程次要剖析出哪些是动态节点,给其打一个标记,为后续更新渲染能够间接跳过动态节点做优化
深度遍历 AST,查看每个子树的节点元素是否为动态节点或者动态节点根。如果为动态节点,他们生成的 DOM 永远不会扭转,这对运行时模板更新起到了极大的优化作用。
(3)生成代码
const code = generate(ast, options)
generate 将 ast 形象语法树编译成 render 字符串并将动态局部放到 staticRenderFns 中,最初通过 new Function(
` render`)
生成 render 函数。
Proxy 与 Object.defineProperty 优劣比照
Proxy 的劣势如下:
- Proxy 能够间接监听对象而非属性;
- Proxy 能够间接监听数组的变动;
- Proxy 有多达 13 种拦挡办法, 不限于 apply、ownKeys、deleteProperty、has 等等是 Object.defineProperty 不具备的;
- Proxy 返回的是一个新对象, 咱们能够只操作新的对象达到目标, 而 Object.defineProperty 只能遍历对象属性间接批改;
Proxy 作为新规范将受到浏览器厂商重点继续的性能优化,也就是传说中的新规范的性能红利;
Object.defineProperty 的劣势如下:
- 兼容性好,反对 IE9,而 Proxy 的存在浏览器兼容性问题, 而且无奈用 polyfill 磨平,因而 Vue 的作者才申明须要等到下个大版本 (3.0) 能力用 Proxy 重写。
v-show 与 v-if 有什么区别?
v-if 是 真正 的条件渲染,因为它会确保在切换过程中条件块内的事件监听器和子组件适当地被销毁和重建;也是 惰性的:如果在初始渲染时条件为假,则什么也不做——直到条件第一次变为真时,才会开始渲染条件块。
v-show 就简略得多——不论初始条件是什么,元素总是会被渲染,并且只是简略地基于 CSS 的“display”属性进行切换。
所以,v-if 实用于在运行时很少扭转条件,不须要频繁切换条件的场景;v-show 则实用于须要十分频繁切换条件的场景。
谈谈 Vue 和 React 组件化的思维
- 1. 咱们在各个页面开发的时候,会产生很多反复的性能,比方 element 中的 xxxx。像这种纯正非页面的 UI,便成为咱们罕用的 UI 组件,最后的前端组件也就仅仅指的是 UI 组件
- 2. 随着业务逻辑变得越来多是,咱们就想要咱们的组件能够解决很多事,这就是咱们常说的组件化,这个组件就不是 UI 组件了,而是包具体业务的业务组件
- 3. 这种开发思维就是分而治之。最大水平的升高开发难度和保护老本的成果。并且能够多人合作,每个人写不同的组件,最初像撘积木一样的把它形成一个页面
Vue 中 computed 和 watch 有什么区别?
计算属性 computed:
(1)** 反对缓存 **,只有依赖数据发生变化时,才会从新进行计算函数;(2)计算属性内 ** 不反对异步操作 **;(3)计算属性的函数中 ** 都有一个 get**(默认具备,获取计算属性)** 和 set**(手动增加,设置计算属性)办法;(4)计算属性是主动监听依赖值的变动,从而动静返回内容。
侦听属性 watch:
(1)** 不反对缓存 **,只有数据发生变化,就会执行侦听函数;(2)侦听属性内 ** 反对异步操作 **;(3)侦听属性的值 ** 能够是一个对象,接管 handler 回调,deep,immediate 三个属性 **;(3)监听是一个过程,在监听的值变动时,能够触发一个回调,并 ** 做一些其余事件 **。
Watch 中的 deep:true 是如何实现的
当用户指定了
watch
中的 deep 属性为true
时,如果以后监控的值是数组类型。会对对象中的每一项进行求值,此时会将以后watcher
存入到对应属性的依赖中,这样数组中对象发生变化时也会告诉数据更新
源码相干
get () {pushTarget(this) // 先将以后依赖放到 Dep.target 上 | |
let value | |
const vm = this.vm | |
try {value = this.getter.call(vm, vm) | |
} catch (e) {if (this.user) {handleError(e, vm, `getter for watcher "${this.expression}"`) | |
} else {throw e} | |
} finally {if (this.deep) { // 如果须要深度监控 | |
traverse(value) // 会对对象中的每一项取值, 取值时会执行对应的 get 办法 | |
}popTarget()} |
vue-router 中如何爱护路由
剖析
路由爱护在利用开发过程中十分重要,简直每个利用都要做各种路由权限治理,因而相当考查使用者基本功。
体验
全局守卫:
const router = createRouter({...}) | |
| |
router.beforeEach((to, from) => { | |
// ... | |
// 返回 false 以勾销导航 | |
return false | |
}) |
路由独享守卫:
const routes = [ | |
{ | |
path: '/users/:id', | |
component: UserDetails, | |
beforeEnter: (to, from) => { | |
// reject the navigation | |
return false | |
}, | |
}, | |
] |
组件内的守卫:
const UserDetails = { | |
template: `...`, | |
beforeRouteEnter(to, from) {// 在渲染该组件的对应路由被验证前调用}, | |
beforeRouteUpdate(to, from) {// 在以后路由扭转,然而该组件被复用时调用}, | |
beforeRouteLeave(to, from) {// 在导航来到渲染该组件的对应路由时调用}, | |
} |
答复
vue-router
中爱护路由的办法叫做路由守卫,次要用来通过跳转或勾销的形式守卫导航。- 路由守卫有三个级别:
全局
、路由独享
、组件级
。影响范畴由大到小,例如全局的router.beforeEach()
,能够注册一个全局前置守卫,每次路由导航都会通过这个守卫,因而在其外部能够退出管制逻辑决定用户是否能够导航到指标路由;在路由注册的时候能够退出单路由独享的守卫,例如beforeEnter
,守卫只在进入路由时触发,因而只会影响这个路由,管制更准确;咱们还能够为路由组件增加守卫配置,例如beforeRouteEnter
,会在渲染该组件的对应路由被验证前调用,管制的范畴更准确了。 - 用户的任何导航行为都会走
navigate
办法,外部有个guards
队列按程序执行用户注册的守卫钩子函数,如果没有通过验证逻辑则会勾销原有的导航。
原理
runGuardQueue(guards)
链式的执行用户在各级别注册的守卫钩子函数,通过则持续下一个级别的守卫,不通过进入 catch
流程勾销本来导航
// 源码 | |
runGuardQueue(guards) | |
.then(() => { | |
// check global guards beforeEach | |
guards = [] | |
for (const guard of beforeGuards.list()) {guards.push(guardToPromiseFn(guard, to, from)) | |
} | |
guards.push(canceledNavigationCheck) | |
return runGuardQueue(guards) | |
}) | |
.then(() => { | |
// check in components beforeRouteUpdate | |
guards = extractComponentsGuards( | |
updatingRecords, | |
'beforeRouteUpdate', | |
to, | |
from | |
) | |
for (const record of updatingRecords) { | |
record.updateGuards.forEach(guard => {guards.push(guardToPromiseFn(guard, to, from)) | |
}) | |
} | |
guards.push(canceledNavigationCheck) | |
// run the queue of per route beforeEnter guards | |
return runGuardQueue(guards) | |
}) | |
.then(() => { | |
// check the route beforeEnter | |
guards = [] | |
for (const record of to.matched) { | |
// do not trigger beforeEnter on reused views | |
if (record.beforeEnter && !from.matched.includes(record)) {if (isArray(record.beforeEnter)) {for (const beforeEnter of record.beforeEnter) | |
guards.push(guardToPromiseFn(beforeEnter, to, from)) | |
} else {guards.push(guardToPromiseFn(record.beforeEnter, to, from)) | |
} | |
} | |
} | |
guards.push(canceledNavigationCheck) | |
// run the queue of per route beforeEnter guards | |
return runGuardQueue(guards) | |
}) | |
.then(() => {// NOTE: at this point to.matched is normalized and does not contain any () => Promise<Component> | |
// clear existing enterCallbacks, these are added by extractComponentsGuards | |
to.matched.forEach(record => (record.enterCallbacks = {})) | |
// check in-component beforeRouteEnter | |
guards = extractComponentsGuards( | |
enteringRecords, | |
'beforeRouteEnter', | |
to, | |
from | |
) | |
guards.push(canceledNavigationCheck) | |
// run the queue of per route beforeEnter guards | |
return runGuardQueue(guards) | |
}) | |
.then(() => { | |
// check global guards beforeResolve | |
guards = [] | |
for (const guard of beforeResolveGuards.list()) {guards.push(guardToPromiseFn(guard, to, from)) | |
} | |
guards.push(canceledNavigationCheck) | |
return runGuardQueue(guards) | |
}) | |
// catch any navigation canceled | |
.catch(err => | |
isNavigationFailure(err, ErrorTypes.NAVIGATION_CANCELLED) | |
? err | |
: Promise.reject(err) | |
) |
源码地位(opens new window)
Vue data 中某一个属性的值产生扭转后,视图会立刻同步执行从新渲染吗?
不会立刻同步执行从新渲染。Vue 实现响应式并不是数据发生变化之后 DOM 立刻变动,而是按肯定的策略进行 DOM 的更新。Vue 在更新 DOM 时是异步执行的。只有侦听到数据变动,Vue 将开启一个队列,并缓冲在同一事件循环中产生的所有数据变更。
如果同一个 watcher 被屡次触发,只会被推入到队列中一次。这种在缓冲时去除反复数据对于防止不必要的计算和 DOM 操作是十分重要的。而后,在下一个的事件循环 tick 中,Vue 刷新队列并执行理论(已去重的)工作。
v-if 和 v -for 哪个优先级更高
- 实际中不应该把
v-for
和v-if
放一起 - 在
vue2
中,v-for
的优先级是高于v-if
,把它们放在一起,输入的渲染函数中能够看出会先执行循环再判断条件,哪怕咱们只渲染列表中一小部分元素,也得在每次重渲染的时候遍历整个列表,这会比拟节约;另外须要留神的是在vue3
中则齐全相同,v-if
的优先级高于v-for
,所以v-if
执行时,它调用的变量还不存在,就会导致异样 -
通常有两种状况下导致咱们这样做:
- 为了过滤列表中的我的项目 (比方
v-for="user in users" v-if="user.isActive"
)。此时定义一个计算属性 (比方activeUsers
),让其返回过滤后的列表即可(比方users.filter(u=>u.isActive)
) - 为了防止渲染本应该被暗藏的列表 (比方
v-for="user in users" v-if="shouldShowUsers"
)。此时把v-if
挪动至容器元素上 (比方ul
、ol
)或者外面包一层template
即可
- 为了过滤列表中的我的项目 (比方
- 文档中明确指出永远不要把
v-if
和v-for
同时用在同一个元素上,显然这是一个重要的注意事项 - 源码外面对于代码生成的局部,可能清晰的看到是先解决
v-if
还是v-for
,程序上vue2
和vue3
正好相同,因而产生了一些症状的不同,然而不管怎样都是不能把它们写在一起的
vue2.x 源码剖析
在 vue 模板编译的时候,会将指令系统转化成可执行的
render
函数
编写一个 p
标签,同时应用 v-if
与 v-for
<div id="app"> | |
<p v-if="isShow" v-for="item in items"> | |
{{item.title}} | |
</p> | |
</div> |
创立 vue
实例,寄存 isShow
与items
数据
const app = new Vue({ | |
el: "#app", | |
data() { | |
return { | |
items: [{ title: "foo"}, | |
{title: "baz"}] | |
} | |
}, | |
computed: {isShow() {return this.items && this.items.length > 0} | |
} | |
}) |
模板指令的代码都会生成在 render
函数中,通过 app.$options.render
就能失去渲染函数
ƒ anonymous() {with (this) { return | |
_c('div', { attrs: { "id": "app"} }, | |
_l((items), function (item) | |
{return (isShow) ? _c('p', [_v("\n" + _s(item.title) + "\n")]) : _e()}), 0) } | |
} |
_l
是vue
的列表渲染函数,函数外部都会进行一次if
判断- 初步失去论断:
v-for
优先级是比v-i
f 高 - 再将
v-for
与v-if
置于不同标签
<div id="app"> | |
<template v-if="isShow"> | |
<p v-for="item in items">{{item.title}}</p> | |
</template> | |
</div> |
再输入下 render
函数
ƒ anonymous() {with(this){return | |
_c('div',{attrs:{"id":"app"}}, | |
[(isShow)?[_v("\n"), | |
_l((items),function(item){return _c('p',[_v(_s(item.title))])})]:_e()],2)} | |
} |
这时候咱们能够看到,v-for
与 v-if
作用在不同标签时候,是先进行判断,再进行列表的渲染
咱们再在查看下 vue 源码
源码地位:\vue-dev\src\compiler\codegen\index.js
export function genElement (el: ASTElement, state: CodegenState): string {if (el.parent) {el.pre = el.pre || el.parent.pre} | |
if (el.staticRoot && !el.staticProcessed) {return genStatic(el, state) | |
} else if (el.once && !el.onceProcessed) {return genOnce(el, state) | |
} else if (el.for && !el.forProcessed) {return genFor(el, state) | |
} else if (el.if && !el.ifProcessed) {return genIf(el, state) | |
} else if (el.tag === 'template' && !el.slotTarget && !state.pre) {return genChildren(el, state) || 'void 0' | |
} else if (el.tag === 'slot') {return genSlot(el, state) | |
} else { | |
// component or element | |
... | |
} |
在进行 if
判断的时候,v-for
是比 v-if
先进行判断
最终论断:v-for
优先级比 v-if
高
为什么要应用异步组件
- 节俭打包出的后果,异步组件离开打包,采纳
jsonp
的形式进行加载,无效解决文件过大的问题。 - 外围就是包组件定义变成一个函数,依赖
import()
语法,能够实现文件的宰割加载。
components:{AddCustomerSchedule:(resolve)=>import("../components/AddCustomer") // require([]) | |
} |
原理
export function (Ctor: Class<Component> | Function | Object | void, data: ?VNodeData, context: Component, children: ?Array<VNode>, tag?: string): VNode | Array<VNode> | void { | |
// async component | |
let asyncFactory | |
if (isUndef(Ctor.cid)) { | |
asyncFactory = Ctor | |
Ctor = resolveAsyncComponent(asyncFactory, baseCtor) // 默认调用此函数时返回 undefiend | |
// 第二次渲染时 Ctor 不为 undefined | |
if (Ctor === undefined) { | |
return createAsyncPlaceholder( // 渲染占位符 空虚构节点 | |
asyncFactory, | |
data, | |
context, | |
children, | |
tag | |
) | |
} | |
} | |
} | |
function resolveAsyncComponent (factory: Function, baseCtor: Class<Component>): Class<Component> | void {if (isDef(factory.resolved)) { | |
// 3. 在次渲染时能够拿到获取的最新组件 | |
return factory.resolved | |
} | |
const resolve = once((res: Object | Class<Component>) => {factory.resolved = ensureCtor(res, baseCtor) | |
if (!sync) {forceRender(true) //2. 强制更新视图从新渲染 | |
} else {owners.length = 0} | |
}) | |
const reject = once(reason => {if (isDef(factory.errorComp)) {factory.error = true forceRender(true) | |
} | |
}) | |
const res = factory(resolve, reject)// 1. 将 resolve 办法和 reject 办法传入,用户调用 resolve 办法后 | |
sync = false | |
return factory.resolved | |
} |
computed 的实现原理
computed 实质是一个惰性求值的观察者。
computed 外部实现了一个惰性的 watcher, 也就是 computed watcher,computed watcher 不会立即求值, 同时持有一个 dep 实例。
其外部通过 this.dirty 属性标记计算属性是否须要从新求值。
当 computed 的依赖状态产生扭转时, 就会告诉这个惰性的 watcher,
computed watcher 通过 this.dep.subs.length 判断有没有订阅者,
有的话, 会从新计算, 而后比照新旧值, 如果变动了, 会从新渲染。(Vue 想确保不仅仅是计算属性依赖的值发生变化,而是当计算属性最终计算的值发生变化时才会触发渲染 watcher 从新渲染,实质上是一种优化。)
没有的话, 仅仅把 this.dirty = true。(当计算属性依赖于其余数据时,属性并不会立刻从新计算,只有之后其余中央须要读取属性的时候,它才会真正计算,即具备 lazy(懒计算)个性。)
Vue-router 跳转和 location.href 有什么区别
- 应用
location.href= /url
来跳转,简略不便,然而刷新了页面; - 应用
history.pushState(/url)
,无刷新页面,动态跳转; - 引进 router,而后应用
router.push(/url)
来跳转,应用了diff
算法,实现了按需加载,缩小了 dom 的耗费。其实应用 router 跳转和应用history.pushState()
没什么差异的,因为 vue-router 就是用了history.pushState()
,尤其是在 history 模式下。
$nextTick 原理及作用
Vue 的 nextTick 其本质是对 JavaScript 执行原理 EventLoop 的一种利用。
nextTick 的外围是利用了如 Promise、MutationObserver、setImmediate、setTimeout 的原生 JavaScript 办法来模仿对应的微 / 宏工作的实现,实质是为了利用 JavaScript 的这些异步回调工作队列来实现 Vue 框架中本人的异步回调队列。
nextTick 不仅是 Vue 外部的异步队列的调用办法,同时也容许开发者在理论我的项目中应用这个办法来满足理论利用中对 DOM 更新数据机会的后续逻辑解决
nextTick 是典型的将底层 JavaScript 执行原理利用到具体案例中的示例,引入异步更新队列机制的起因∶
- 如果是同步更新,则屡次对一个或多个属性赋值,会频繁触发 UI/DOM 的渲染,能够缩小一些无用渲染
- 同时因为 VirtualDOM 的引入,每一次状态发生变化后,状态变动的信号会发送给组件,组件外部应用 VirtualDOM 进行计算得出须要更新的具体的 DOM 节点,而后对 DOM 进行更新操作,每次更新状态后的渲染过程须要更多的计算,而这种无用功也将节约更多的性能,所以异步渲染变得更加至关重要
Vue 采纳了数据驱动视图的思维,然而在一些状况下,依然须要操作 DOM。有时候,可能遇到这样的状况,DOM1 的数据产生了变动,而 DOM2 须要从 DOM1 中获取数据,那这时就会发现 DOM2 的视图并没有更新,这时就须要用到了 nextTick
了。
因为 Vue 的 DOM 操作是异步的,所以,在下面的状况中,就要将 DOM2 获取数据的操作写在 $nextTick
中。
this.$nextTick(() => { // 获取数据的操作...})
所以,在以下状况下,会用到 nextTick:
- 在数据变动后执行的某个操作,而这个操作须要应用随数据变动而变动的 DOM 构造的时候,这个操作就须要办法在
nextTick()
的回调函数中。 - 在 vue 生命周期中,如果在 created()钩子进行 DOM 操作,也肯定要放在
nextTick()
的回调函数中。
因为在 created()钩子函数中,页面的 DOM 还未渲染,这时候也没方法操作 DOM,所以,此时如果想要操作 DOM,必须将操作的代码放在 nextTick()
的回调函数中。
Vue3.0 和 2.0 的响应式原理区别
Vue3.x 改用 Proxy 代替 Object.defineProperty。因为 Proxy 能够间接监听对象和数组的变动,并且有多达 13 种拦挡办法。
相干代码如下
import {mutableHandlers} from "./baseHandlers"; // 代理相干逻辑 | |
import {isObject} from "./util"; // 工具办法 | |
export function reactive(target) { | |
// 依据不同参数创立不同响应式对象 | |
return createReactiveObject(target, mutableHandlers); | |
} | |
function createReactiveObject(target, baseHandler) {if (!isObject(target)) {return target;} | |
const observed = new Proxy(target, baseHandler); | |
return observed; | |
} | |
const get = createGetter(); | |
const set = createSetter(); | |
function createGetter() {return function get(target, key, receiver) { | |
// 对获取的值进行喷射 | |
const res = Reflect.get(target, key, receiver); | |
console.log("属性获取", key); | |
if (isObject(res)) { | |
// 如果获取的值是对象类型,则返回以后对象的代理对象 | |
return reactive(res); | |
} | |
return res; | |
}; | |
} | |
function createSetter() {return function set(target, key, value, receiver) {const oldValue = target[key]; | |
const hadKey = hasOwn(target, key); | |
const result = Reflect.set(target, key, value, receiver); | |
if (!hadKey) {console.log("属性新增", key, value); | |
} else if (hasChanged(value, oldValue)) {console.log("属性值被批改", key, value); | |
} | |
return result; | |
}; | |
} | |
export const mutableHandlers = { | |
get, // 当获取属性时调用此办法 | |
set, // 当批改属性时调用此办法 | |
}; |
vue3 中 watch、watchEffect 区别
watch
是惰性执行,也就是只有监听的值发生变化的时候才会执行,然而watchEffect
不同,每次代码加载watchEffect
都会执行(疏忽watch
第三个参数的配置,如果批改配置项也能够实现立刻执行)watch
须要传递监听的对象,watchEffect
不须要watch
只能监听响应式数据:ref
定义的属性和reactive
定义的对象,如果间接监听reactive
定义对象中的属性是不容许的(会报正告),除非应用函数转换一下。其实就是官网上说的监听一个getter
watchEffect
如果监听reactive
定义的对象是不起作用的,只能监听对象中的属性
看一下 watchEffect
的代码
<template> | |
<div> | |
请输出 firstName:<input type="text" v-model="firstName"> | |
</div> | |
<div> | |
请输出 lastName:<input type="text" v-model="lastName"> | |
</div> | |
<div> | |
请输出 obj.text:<input type="text" v-model="obj.text"> | |
</div> | |
<div>【obj.text】{{obj.text}} | |
</div> | |
</template> | |
<script> | |
import {ref, reactive, watch, watchEffect} from 'vue' | |
export default { | |
name: "HelloWorld", | |
props: {msg: String,}, | |
setup(props,content){let firstName = ref('') | |
let lastName = ref('') | |
let obj= reactive({text:'hello'}) | |
watchEffect(()=>{console.log('触发了 watchEffect'); | |
console.log(` 组合后的名称为:${firstName.value}${lastName.value}`) | |
}) | |
return{ | |
obj, | |
firstName, | |
lastName | |
} | |
} | |
}; | |
</script> |
革新一下代码
watchEffect(()=>{console.log('触发了 watchEffect'); | |
// 这里咱们不应用 firstName.value/lastName.value,相当于是监控整个 ref, 对应第四点下面的论断 | |
console.log(` 组合后的名称为:${firstName}${lastName}`) | |
}) |
watchEffect(()=>{console.log('触发了 watchEffect'); | |
console.log(obj); | |
}) |
略微革新一下
let obj = reactive({text:'hello'}) | |
watchEffect(()=>{console.log('触发了 watchEffect'); | |
console.log(obj.text); | |
}) |
再看一下 watch 的代码,验证一下
let obj= reactive({text:'hello'}) | |
// watch 是惰性执行,默认初始化之后不会执行,只有值有变动才会触发,可通过配置参数实现默认执行 | |
watch(obj, (newValue, oldValue) => { | |
// 回调函数 | |
console.log('触发监控更新了 new', newValue); | |
console.log('触发监控更新了 old', oldValue); | |
},{ | |
// 配置 immediate 参数,立刻执行,以及深层次监听 | |
immediate: true, | |
deep: true | |
}) |
- 监控整个
reactive
对象,从下面的图能够看到deep
理论默认是开启的,就算咱们设置为false
也还是有效。而且旧值获取不到。 - 要获取旧值则须要监控对象的属性,也就是监听一个
getter
,看下图
总结
- 如果定义了
reactive
的数据,想去应用watch
监听数据扭转,则无奈正确获取旧值,并且deep
属性配置有效,主动强制开启了深层次监听。 - 如果应用
ref
初始化一个对象或者数组类型的数据,会被主动转成reactive
的实现形式,生成proxy
代理对象。也会变得无奈正确取旧值。 - 用任何形式生成的数据,如果接管的变量是一个
proxy
代理对象,就都会导致watch
这个对象时,watch
回调里无奈正确获取旧值。 - 所以当大家应用
watch
监听对象时,如果在不须要应用旧值的状况,能够失常监听对象没关系;然而如果当监听扭转函数外面须要用到旧值时,只能监听 对象.xxx` 属性 的形式才行
watch 和 watchEffect 异同总结
体验
watchEffect
立刻运行一个函数,而后被动地追踪它的依赖,当这些依赖扭转时从新执行该函数
const count = ref(0) | |
| |
watchEffect(() => console.log(count.value)) | |
// -> logs 0 | |
| |
count.value++ | |
// -> logs 1 |
watch
侦测一个或多个响应式数据源并在数据源变动时调用一个回调函数
const state = reactive({count: 0}) | |
watch(() => state.count, | |
(count, prevCount) => {/* ... */} | |
) |
答复范例
watchEffect
立刻运行一个函数,而后被动地追踪它的依赖,当这些依赖扭转时从新执行该函数。watch
侦测一个或多个响应式数据源并在数据源变动时调用一个回调函数watchEffect(effect)
是一种非凡watch
,传入的函数既是依赖收集的数据源,也是回调函数。如果咱们不关怀响应式数据变动前后的值,只是想拿这些数据做些事件,那么watchEffect
就是咱们须要的。watch
更底层,能够接管多种数据源,包含用于依赖收集的getter
函数,因而它齐全能够实现watchEffect
的性能,同时因为能够指定getter
函数,依赖能够管制的更准确,还能获取数据变动前后的值,因而如果须要这些时咱们会应用watch
watchEffect
在应用时,传入的函数会立即执行一次。watch
默认状况下并不会执行回调函数,除非咱们手动设置immediate
选项- 从实现上来说,
watchEffect(fn)
相当于watch(fn,fn,{immediate:true})
watchEffect
定义如下
export function watchEffect( | |
effect: WatchEffect, | |
options?: WatchOptionsBase | |
): WatchStopHandle {return doWatch(effect, null, options) | |
} |
watch
定义如下
export function watch<T = any, Immediate extends Readonly<boolean> = false>( | |
source: T | WatchSource<T>, | |
cb: any, | |
options?: WatchOptions<Immediate> | |
): WatchStopHandle {return doWatch(source as any, cb, options) | |
} |
很显著 watchEffect
就是一种非凡的 watch
实现。
nextTick 应用场景和原理
nextTick 中的回调是在下次 DOM 更新循环完结之后执行的提早回调。在批改数据之后立刻应用这个办法,获取更新后的 DOM。次要思路就是采纳微工作优先的形式调用异步办法去执行 nextTick 包装的办法
相干代码如下
let callbacks = []; | |
let pending = false; | |
function flushCallbacks() { | |
pending = false; // 把标记还原为 false | |
// 顺次执行回调 | |
for (let i = 0; i < callbacks.length; i++) {callbacks[i]();} | |
} | |
let timerFunc; // 定义异步办法 采纳优雅降级 | |
if (typeof Promise !== "undefined") { | |
// 如果反对 promise | |
const p = Promise.resolve(); | |
timerFunc = () => {p.then(flushCallbacks); | |
}; | |
} else if (typeof MutationObserver !== "undefined") { | |
// MutationObserver 次要是监听 dom 变动 也是一个异步办法 | |
let counter = 1; | |
const observer = new MutationObserver(flushCallbacks); | |
const textNode = document.createTextNode(String(counter)); | |
observer.observe(textNode, {characterData: true,}); | |
timerFunc = () => {counter = (counter + 1) % 2; | |
textNode.data = String(counter); | |
}; | |
} else if (typeof setImmediate !== "undefined") { | |
// 如果后面都不反对 判断 setImmediate | |
timerFunc = () => {setImmediate(flushCallbacks); | |
}; | |
} else { | |
// 最初降级采纳 setTimeout | |
timerFunc = () => {setTimeout(flushCallbacks, 0); | |
}; | |
} | |
export function nextTick(cb) { | |
// 除了渲染 watcher 还有用户本人手动调用的 nextTick 一起被收集到数组 | |
callbacks.push(cb); | |
if (!pending) { | |
// 如果屡次调用 nextTick 只会执行一次异步 等异步队列清空之后再把标记变为 false | |
pending = true; | |
timerFunc();} | |
} |
vue2.x 具体
1. 剖析
首先找到 vue
的构造函数
源码地位:src\core\instance\index.js
function Vue (options) { | |
if (process.env.NODE_ENV !== 'production' && | |
!(this instanceof Vue) | |
) {warn('Vue is a constructor and should be called with the `new` keyword') | |
} | |
this._init(options) | |
} |
options
是用户传递过去的配置项,如 data、methods
等罕用的办法
vue
构建函数调用 _init
办法,但咱们发现本文件中并没有此办法,但认真能够看到文件下方定定义了很多初始化办法
initMixin(Vue); // 定义 _init | |
stateMixin(Vue); // 定义 $set $get $delete $watch 等 | |
eventsMixin(Vue); // 定义事件 $on $once $off $emit | |
lifecycleMixin(Vue);// 定义 _update $forceUpdate $destroy | |
renderMixin(Vue); // 定义 _render 返回虚构 dom |
首先能够看 initMixin
办法,发现该办法在 Vue
原型上定义了 _init
办法
源码地位:src\core\instance\init.js
Vue.prototype._init = function (options?: Object) { | |
const vm: Component = this | |
// a uid | |
vm._uid = uid++ | |
let startTag, endTag | |
/* istanbul ignore if */ | |
if (process.env.NODE_ENV !== 'production' && config.performance && mark) {startTag = `vue-perf-start:${vm._uid}` | |
endTag = `vue-perf-end:${vm._uid}` | |
mark(startTag) | |
} | |
// a flag to avoid this being observed | |
vm._isVue = true | |
// merge options | |
// 合并属性,判断初始化的是否是组件,这里合并次要是 mixins 或 extends 的办法 | |
if (options && options._isComponent) { | |
// optimize internal component instantiation | |
// since dynamic options merging is pretty slow, and none of the | |
// internal component options needs special treatment. | |
initInternalComponent(vm, options) | |
} else { // 合并 vue 属性 | |
vm.$options = mergeOptions(resolveConstructorOptions(vm.constructor), | |
options || {}, | |
vm | |
) | |
} | |
/* istanbul ignore else */ | |
if (process.env.NODE_ENV !== 'production') { | |
// 初始化 proxy 拦截器 | |
initProxy(vm) | |
} else {vm._renderProxy = vm} | |
// expose real self | |
vm._self = vm | |
// 初始化组件生命周期标记位 | |
initLifecycle(vm) | |
// 初始化组件事件侦听 | |
initEvents(vm) | |
// 初始化渲染办法 | |
initRender(vm) | |
callHook(vm, 'beforeCreate') | |
// 初始化依赖注入内容,在初始化 data、props 之前 | |
initInjections(vm) // resolve injections before data/props | |
// 初始化 props/data/method/watch/methods | |
initState(vm) | |
initProvide(vm) // resolve provide after data/props | |
callHook(vm, 'created') | |
/* istanbul ignore if */ | |
if (process.env.NODE_ENV !== 'production' && config.performance && mark) {vm._name = formatComponentName(vm, false) | |
mark(endTag) | |
measure(`vue ${vm._name} init`, startTag, endTag) | |
} | |
// 挂载元素 | |
if (vm.$options.el) {vm.$mount(vm.$options.el) | |
} | |
} |
仔细阅读下面的代码,咱们失去以下论断:
- 在调用
beforeCreate
之前,数据初始化并未实现,像data
、props
这些属性无法访问到 - 到了
created
的时候,数据曾经初始化实现,可能拜访data
、props
这些属性,但这时候并未实现dom
的挂载,因而无法访问到dom
元素 - 挂载办法是调用
vm.$mount
办法
initState
办法是实现 props/data/method/watch/methods
的初始化
源码地位:src\core\instance\state.js
export function initState (vm: Component) { | |
// 初始化组件的 watcher 列表 | |
vm._watchers = [] | |
const opts = vm.$options | |
// 初始化 props | |
if (opts.props) initProps(vm, opts.props) | |
// 初始化 methods 办法 | |
if (opts.methods) initMethods(vm, opts.methods) | |
if (opts.data) { | |
// 初始化 data | |
initData(vm) | |
} else {observe(vm._data = {}, true /* asRootData */) | |
} | |
if (opts.computed) initComputed(vm, opts.computed) | |
if (opts.watch && opts.watch !== nativeWatch) {initWatch(vm, opts.watch) | |
} | |
} |
咱们和这里次要看初始化 data
的办法为 initData
,它与initState
在同一文件上
function initData (vm: Component) { | |
let data = vm.$options.data | |
// 获取到组件上的 data | |
data = vm._data = typeof data === 'function' | |
? getData(data, vm) | |
: data || {} | |
if (!isPlainObject(data)) {data = {} | |
process.env.NODE_ENV !== 'production' && warn( | |
'data functions should return an object:\n' + | |
'https://vuejs.org/v2/guide/components.html#data-Must-Be-a-Function', | |
vm | |
) | |
} | |
// proxy data on instance | |
const keys = Object.keys(data) | |
const props = vm.$options.props | |
const methods = vm.$options.methods | |
let i = keys.length | |
while (i--) {const key = keys[i] | |
if (process.env.NODE_ENV !== 'production') { | |
// 属性名不能与办法名反复 | |
if (methods && hasOwn(methods, key)) { | |
warn(`Method "${key}" has already been defined as a data property.`, | |
vm | |
) | |
} | |
} | |
// 属性名不能与 state 名称反复 | |
if (props && hasOwn(props, key)) { | |
process.env.NODE_ENV !== 'production' && warn(`The data property "${key}" is already declared as a prop. ` + | |
`Use prop default value instead.`, | |
vm | |
) | |
} else if (!isReserved(key)) { // 验证 key 值的合法性 | |
// 将_data 中的数据挂载到组件 vm 上, 这样就能够通过 this.xxx 拜访到组件上的数据 | |
proxy(vm, `_data`, key) | |
} | |
} | |
// observe data | |
// 响应式监听 data 是数据的变动 | |
observe(data, true /* asRootData */) | |
} |
仔细阅读下面的代码,咱们能够失去以下论断:
- 初始化程序:
props
、methods
、data
data
定义的时候可选择函数模式或者对象模式(组件只能为函数模式)
对于数据响应式在这就不开展具体阐明
上文提到挂载办法是调用 vm.$mount
办法
源码地位:
Vue.prototype.$mount = function ( | |
el?: string | Element, | |
hydrating?: boolean | |
): Component { | |
// 获取或查问元素 | |
el = el && query(el) | |
/* istanbul ignore if */ | |
// vue 不容许间接挂载到 body 或页面文档上 | |
if (el === document.body || el === document.documentElement) { | |
process.env.NODE_ENV !== 'production' && warn(`Do not mount Vue to <html> or <body> - mount to normal elements instead.`) | |
return this | |
} | |
const options = this.$options | |
// resolve template/el and convert to render function | |
if (!options.render) { | |
let template = options.template | |
// 存在 template 模板,解析 vue 模板文件 | |
if (template) {if (typeof template === 'string') {if (template.charAt(0) === '#') {template = idToTemplate(template) | |
/* istanbul ignore if */ | |
if (process.env.NODE_ENV !== 'production' && !template) { | |
warn(`Template element not found or is empty: ${options.template}`, | |
this | |
) | |
} | |
} | |
} else if (template.nodeType) {template = template.innerHTML} else {if (process.env.NODE_ENV !== 'production') {warn('invalid template option:' + template, this) | |
} | |
return this | |
} | |
} else if (el) { | |
// 通过选择器获取元素内容 | |
template = getOuterHTML(el) | |
} | |
if (template) { | |
/* istanbul ignore if */ | |
if (process.env.NODE_ENV !== 'production' && config.performance && mark) {mark('compile') | |
} | |
/** | |
* 1. 将 temmplate 解析 ast tree | |
* 2. 将 ast tree 转换成 render 语法字符串 | |
* 3. 生成 render 办法 | |
*/ | |
const {render, staticRenderFns} = compileToFunctions(template, { | |
outputSourceRange: process.env.NODE_ENV !== 'production', | |
shouldDecodeNewlines, | |
shouldDecodeNewlinesForHref, | |
delimiters: options.delimiters, | |
comments: options.comments | |
}, this) | |
options.render = render | |
options.staticRenderFns = staticRenderFns | |
/* istanbul ignore if */ | |
if (process.env.NODE_ENV !== 'production' && config.performance && mark) {mark('compile end') | |
measure(`vue ${this._name} compile`, 'compile', 'compile end') | |
} | |
} | |
} | |
return mount.call(this, el, hydrating) | |
} |
浏览下面代码,咱们能失去以下论断:
- 不要将根元素放到
body
或者html
上 - 能够在对象中定义
template/render
或者间接应用template
、el
示意元素选择器 - 最终都会解析成
render
函数,调用compileToFunctions
,会将template
解析成render
函数
对 template
的解析步骤大抵分为以下几步:
- 将
html
文档片段解析成ast
描述符 - 将
ast
描述符解析成字符串 - 生成
render
函数
生成 render
函数,挂载到 vm
上后,会再次调用 mount
办法
源码地位:src\platforms\web\runtime\index.js
// public mount method | |
Vue.prototype.$mount = function ( | |
el?: string | Element, | |
hydrating?: boolean | |
): Component {el = el && inBrowser ? query(el) : undefined | |
// 渲染组件 | |
return mountComponent(this, el, hydrating) | |
} |
调用 mountComponent
渲染组件
export function mountComponent ( | |
vm: Component, | |
el: ?Element, | |
hydrating?: boolean | |
): Component { | |
vm.$el = el | |
// 如果没有获取解析的 render 函数,则会抛出正告 | |
// render 是解析模板文件生成的 | |
if (!vm.$options.render) { | |
vm.$options.render = createEmptyVNode | |
if (process.env.NODE_ENV !== 'production') { | |
/* istanbul ignore if */ | |
if ((vm.$options.template && vm.$options.template.charAt(0) !== '#') || | |
vm.$options.el || el) { | |
warn( | |
'You are using the runtime-only build of Vue where the template' + | |
'compiler is not available. Either pre-compile the templates into' + | |
'render functions, or use the compiler-included build.', | |
vm | |
) | |
} else { | |
// 没有获取到 vue 的模板文件 | |
warn( | |
'Failed to mount component: template or render function not defined.', | |
vm | |
) | |
} | |
} | |
} | |
// 执行 beforeMount 钩子 | |
callHook(vm, 'beforeMount') | |
let updateComponent | |
/* istanbul ignore if */ | |
if (process.env.NODE_ENV !== 'production' && config.performance && mark) {updateComponent = () => { | |
const name = vm._name | |
const id = vm._uid | |
const startTag = `vue-perf-start:${id}` | |
const endTag = `vue-perf-end:${id}` | |
mark(startTag) | |
const vnode = vm._render() | |
mark(endTag) | |
measure(`vue ${name} render`, startTag, endTag) | |
mark(startTag) | |
vm._update(vnode, hydrating) | |
mark(endTag) | |
measure(`vue ${name} patch`, startTag, endTag) | |
} | |
} else { | |
// 定义更新函数 | |
updateComponent = () => { | |
// 理论调⽤是在 lifeCycleMixin 中定义的_update 和 renderMixin 中定义的_render | |
vm._update(vm._render(), hydrating) | |
} | |
} | |
// we set this to vm._watcher inside the watcher's constructor | |
// since the watcher's initial patch may call $forceUpdate (e.g. inside child | |
// component's mounted hook), which relies on vm._watcher being already defined | |
// 监听以后组件状态,当有数据变动时,更新组件 | |
new Watcher(vm, updateComponent, noop, {before () {if (vm._isMounted && !vm._isDestroyed) { | |
// 数据更新引发的组件更新 | |
callHook(vm, 'beforeUpdate') | |
} | |
} | |
}, true /* isRenderWatcher */) | |
hydrating = false | |
// manually mounted instance, call mounted on self | |
// mounted is called for render-created child components in its inserted hook | |
if (vm.$vnode == null) { | |
vm._isMounted = true | |
callHook(vm, 'mounted') | |
} | |
return vm | |
} |
浏览下面代码,咱们失去以下论断:
- 会触发
boforeCreate
钩子 - 定义
updateComponent
渲染页面视图的办法 - 监听组件数据,一旦发生变化,触发
beforeUpdate
生命钩子
updateComponent
办法次要执行在 vue
初始化时申明的 render
,update
办法
render的作用次要是生成
vnode
源码地位:src\core\instance\render.js
// 定义 vue 原型上的 render 办法 | |
Vue.prototype._render = function (): VNode { | |
const vm: Component = this | |
// render 函数来自于组件的 option | |
const {render, _parentVnode} = vm.$options | |
if (_parentVnode) { | |
vm.$scopedSlots = normalizeScopedSlots( | |
_parentVnode.data.scopedSlots, | |
vm.$slots, | |
vm.$scopedSlots | |
) | |
} | |
// set parent vnode. this allows render functions to have access | |
// to the data on the placeholder node. | |
vm.$vnode = _parentVnode | |
// render self | |
let vnode | |
try { | |
// There's no need to maintain a stack because all render fns are called | |
// separately from one another. Nested component's render fns are called | |
// when parent component is patched. | |
currentRenderingInstance = vm | |
// 调用 render 办法,本人的独特的 render 办法,传入 createElement 参数,生成 vNode | |
vnode = render.call(vm._renderProxy, vm.$createElement) | |
} catch (e) {handleError(e, vm, `render`) | |
// return error render result, | |
// or previous vnode to prevent render error causing blank component | |
/* istanbul ignore else */ | |
if (process.env.NODE_ENV !== 'production' && vm.$options.renderError) { | |
try {vnode = vm.$options.renderError.call(vm._renderProxy, vm.$createElement, e) | |
} catch (e) {handleError(e, vm, `renderError`) | |
vnode = vm._vnode | |
} | |
} else {vnode = vm._vnode} | |
} finally {currentRenderingInstance = null} | |
// if the returned array contains only a single node, allow it | |
if (Array.isArray(vnode) && vnode.length === 1) {vnode = vnode[0] | |
} | |
// return empty vnode in case the render function errored out | |
if (!(vnode instanceof VNode)) {if (process.env.NODE_ENV !== 'production' && Array.isArray(vnode)) { | |
warn( | |
'Multiple root nodes returned from render function. Render function' + | |
'should return a single root node.', | |
vm | |
) | |
} | |
vnode = createEmptyVNode()} | |
// set parent | |
vnode.parent = _parentVnode | |
return vnode | |
} |
_update
次要性能是调用 patch
,将vnode
转换为实在DOM
,并且更新到页面中
源码地位:src\core\instance\lifecycle.js
Vue.prototype._update = function (vnode: VNode, hydrating?: boolean) { | |
const vm: Component = this | |
const prevEl = vm.$el | |
const prevVnode = vm._vnode | |
// 设置以后激活的作用域 | |
const restoreActiveInstance = setActiveInstance(vm) | |
vm._vnode = vnode | |
// Vue.prototype.__patch__ is injected in entry points | |
// based on the rendering backend used. | |
if (!prevVnode) { | |
// initial render | |
// 执行具体的挂载逻辑 | |
vm.$el = vm.__patch__(vm.$el, vnode, hydrating, false /* removeOnly */) | |
} else { | |
// updates | |
vm.$el = vm.__patch__(prevVnode, vnode) | |
} | |
restoreActiveInstance() | |
// update __vue__ reference | |
if (prevEl) {prevEl.__vue__ = null} | |
if (vm.$el) {vm.$el.__vue__ = vm} | |
// if parent is an HOC, update its $el as well | |
if (vm.$vnode && vm.$parent && vm.$vnode === vm.$parent._vnode) {vm.$parent.$el = vm.$el} | |
// updated hook is called by the scheduler to ensure that children are | |
// updated in a parent's updated hook. | |
} |
2. 论断
-
new Vue
的时候调用会调用_init
办法- 定义
$set
、$get
、$delete
、$watch
等办法 - 定义
$on
、$off
、$emit
、$off
等事件 - 定义
_update
、$forceUpdate
、$destroy
生命周期
- 定义
- 调用
$mount
进行页面的挂载 - 挂载的时候次要是通过
mountComponent
办法 - 定义
updateComponent
更新函数 - 执行
render
生成虚构DOM
_update
将虚构DOM
生成实在DOM
构造,并且渲染到页面中
created 和 mounted 的区别
- created: 在模板渲染成 html 前调用,即通常初始化某些属性值,而后再渲染成视图。
- mounted: 在模板渲染成 html 后调用,通常是初始化页面实现后,再对 html 的 dom 节点进行一些须要的操作。
Vue 中封装的数组办法有哪些,其如何实现页面更新
在 Vue 中,对响应式解决利用的是 Object.defineProperty 对数据进行拦挡,而这个办法并不能监听到数组外部变动,数组长度变动,数组的截取变动等,所以须要对这些操作进行 hack,让 Vue 能监听到其中的变动。那 Vue 是如何实现让这些数组办法实现元素的实时更新的呢,上面是 Vue 中对这些办法的封装:
// 缓存数组原型 | |
const arrayProto = Array.prototype; | |
// 实现 arrayMethods.__proto__ === Array.prototype | |
export const arrayMethods = Object.create(arrayProto); | |
// 须要进行性能拓展的办法 | |
const methodsToPatch = [ | |
"push", | |
"pop", | |
"shift", | |
"unshift", | |
"splice", | |
"sort", | |
"reverse" | |
]; | |
/** * Intercept mutating methods and emit events */ | |
methodsToPatch.forEach(function(method) { | |
// 缓存原生数组办法 | |
const original = arrayProto[method]; | |
def(arrayMethods, method, function mutator(...args) { | |
// 执行并缓存原生数组性能 | |
const result = original.apply(this, args); | |
// 响应式解决 | |
const ob = this.__ob__; | |
let inserted; | |
switch (method) { | |
// push、unshift 会新增索引,所以要手动 observer | |
case "push": | |
case "unshift": | |
inserted = args; | |
break; | |
// splice 办法,如果传入了第三个参数,也会有索引退出,也要手动 observer。case "splice": | |
inserted = args.slice(2); | |
break; | |
} | |
// | |
if (inserted) ob.observeArray(inserted);// 获取插入的值,并设置响应式监听 | |
// notify change | |
ob.dep.notify();// 告诉依赖更新 | |
// 返回原生数组办法的执行后果 | |
return result; | |
}); | |
}); |
简略来说就是,重写了数组中的那些原生办法,首先获取到这个数组的__ob__,也就是它的 Observer 对象,如果有新的值,就调用 observeArray 持续对新的值察看变动(也就是通过 target__proto__ == arrayMethods
来扭转了数组实例的型),而后手动调用 notify,告诉渲染 watcher,执行 update。
参考:前端 vue 面试题具体解答
vue 如何监听对象或者数组某个属性的变动
当在我的项目中间接设置数组的某一项的值,或者间接设置对象的某个属性值,这个时候,你会发现页面并没有更新。这是因为 Object.defineProperty()限度,监听不到变动。
解决形式:
- this.$set(你要扭转的数组 / 对象,你要扭转的地位 /key,你要改成什么 value)
this.$set(this.arr, 0, "OBKoro1"); // 扭转数组 this.$set(this.obj, "c", "OBKoro1"); // 扭转对象
- 调用以下几个数组的办法
splice()、push()、pop()、shift()、unshift()、sort()、reverse()
vue 源码里缓存了 array 的原型链,而后重写了这几个办法,触发这几个办法的时候会 observer 数据,意思是应用这些办法不必再进行额定的操作,视图主动进行更新。举荐应用 splice 办法会比拟好自定义, 因为 splice 能够在数组的任何地位进行删除 / 增加操作
vm.$set
的实现原理是:
- 如果指标是数组,间接应用数组的 splice 办法触发相应式;
- 如果指标是对象,会先判读属性是否存在、对象是否是响应式,最终如果要对属性进行响应式解决,则是通过调用 defineReactive 办法进行响应式解决(defineReactive 办法就是 Vue 在初始化对象时,给对象属性采纳 Object.defineProperty 动静增加 getter 和 setter 的性能所调用的办法)
Vue template 到 render 的过程
vue 的模版编译过程次要如下:template -> ast -> render 函数
vue 在模版编译版本的码中会执行 compileToFunctions 将 template 转化为 render 函数:
// 将模板编译为 render 函数 const {render, staticRenderFns} = compileToFunctions(template,options// 省略}, this)
CompileToFunctions 中的次要逻辑如下∶ (1)调用 parse 办法将 template 转化为 ast(形象语法树)
constast = parse(template.trim(), options)
- parse 的指标:把 tamplate 转换为 AST 树,它是一种用 JavaScript 对象的模式来形容整个模板。
- 解析过程:利用正则表达式程序解析模板,当解析到开始标签、闭合标签、文本的时候都会别离执行对应的 回调函数,来达到结构 AST 树的目标。
AST 元素节点总共三种类型:type 为 1 示意一般元素、2 为表达式、3 为纯文本
(2)对动态节点做优化
optimize(ast,options)
这个过程次要剖析出哪些是动态节点,给其打一个标记,为后续更新渲染能够间接跳过动态节点做优化
深度遍历 AST,查看每个子树的节点元素是否为动态节点或者动态节点根。如果为动态节点,他们生成的 DOM 永远不会扭转,这对运行时模板更新起到了极大的优化作用。
(3)生成代码
const code = generate(ast, options)
generate 将 ast 形象语法树编译成 render 字符串并将动态局部放到 staticRenderFns 中,最初通过 new Function(
` render`)
生成 render 函数。
Proxy 与 Object.defineProperty 优劣比照
Proxy 的劣势如下:
- Proxy 能够间接监听对象而非属性;
- Proxy 能够间接监听数组的变动;
- Proxy 有多达 13 种拦挡办法, 不限于 apply、ownKeys、deleteProperty、has 等等是 Object.defineProperty 不具备的;
- Proxy 返回的是一个新对象, 咱们能够只操作新的对象达到目标, 而 Object.defineProperty 只能遍历对象属性间接批改;
Proxy 作为新规范将受到浏览器厂商重点继续的性能优化,也就是传说中的新规范的性能红利;
Object.defineProperty 的劣势如下:
- 兼容性好,反对 IE9,而 Proxy 的存在浏览器兼容性问题, 而且无奈用 polyfill 磨平,因而 Vue 的作者才申明须要等到下个大版本 (3.0) 能力用 Proxy 重写。
v-show 与 v-if 有什么区别?
v-if 是 真正 的条件渲染,因为它会确保在切换过程中条件块内的事件监听器和子组件适当地被销毁和重建;也是 惰性的:如果在初始渲染时条件为假,则什么也不做——直到条件第一次变为真时,才会开始渲染条件块。
v-show 就简略得多——不论初始条件是什么,元素总是会被渲染,并且只是简略地基于 CSS 的“display”属性进行切换。
所以,v-if 实用于在运行时很少扭转条件,不须要频繁切换条件的场景;v-show 则实用于须要十分频繁切换条件的场景。
谈谈 Vue 和 React 组件化的思维
- 1. 咱们在各个页面开发的时候,会产生很多反复的性能,比方 element 中的 xxxx。像这种纯正非页面的 UI,便成为咱们罕用的 UI 组件,最后的前端组件也就仅仅指的是 UI 组件
- 2. 随着业务逻辑变得越来多是,咱们就想要咱们的组件能够解决很多事,这就是咱们常说的组件化,这个组件就不是 UI 组件了,而是包具体业务的业务组件
- 3. 这种开发思维就是分而治之。最大水平的升高开发难度和保护老本的成果。并且能够多人合作,每个人写不同的组件,最初像撘积木一样的把它形成一个页面
Vue 中 computed 和 watch 有什么区别?
计算属性 computed:
(1)** 反对缓存 **,只有依赖数据发生变化时,才会从新进行计算函数;(2)计算属性内 ** 不反对异步操作 **;(3)计算属性的函数中 ** 都有一个 get**(默认具备,获取计算属性)** 和 set**(手动增加,设置计算属性)办法;(4)计算属性是主动监听依赖值的变动,从而动静返回内容。
侦听属性 watch:
(1)** 不反对缓存 **,只有数据发生变化,就会执行侦听函数;(2)侦听属性内 ** 反对异步操作 **;(3)侦听属性的值 ** 能够是一个对象,接管 handler 回调,deep,immediate 三个属性 **;(3)监听是一个过程,在监听的值变动时,能够触发一个回调,并 ** 做一些其余事件 **。
Watch 中的 deep:true 是如何实现的
当用户指定了
watch
中的 deep 属性为true
时,如果以后监控的值是数组类型。会对对象中的每一项进行求值,此时会将以后watcher
存入到对应属性的依赖中,这样数组中对象发生变化时也会告诉数据更新
源码相干
get () {pushTarget(this) // 先将以后依赖放到 Dep.target 上 | |
let value | |
const vm = this.vm | |
try {value = this.getter.call(vm, vm) | |
} catch (e) {if (this.user) {handleError(e, vm, `getter for watcher "${this.expression}"`) | |
} else {throw e} | |
} finally {if (this.deep) { // 如果须要深度监控 | |
traverse(value) // 会对对象中的每一项取值, 取值时会执行对应的 get 办法 | |
}popTarget()} |
vue-router 中如何爱护路由
剖析
路由爱护在利用开发过程中十分重要,简直每个利用都要做各种路由权限治理,因而相当考查使用者基本功。
体验
全局守卫:
const router = createRouter({...}) | |
| |
router.beforeEach((to, from) => { | |
// ... | |
// 返回 false 以勾销导航 | |
return false | |
}) |
路由独享守卫:
const routes = [ | |
{ | |
path: '/users/:id', | |
component: UserDetails, | |
beforeEnter: (to, from) => { | |
// reject the navigation | |
return false | |
}, | |
}, | |
] |
组件内的守卫:
const UserDetails = { | |
template: `...`, | |
beforeRouteEnter(to, from) {// 在渲染该组件的对应路由被验证前调用}, | |
beforeRouteUpdate(to, from) {// 在以后路由扭转,然而该组件被复用时调用}, | |
beforeRouteLeave(to, from) {// 在导航来到渲染该组件的对应路由时调用}, | |
} |
答复
vue-router
中爱护路由的办法叫做路由守卫,次要用来通过跳转或勾销的形式守卫导航。- 路由守卫有三个级别:
全局
、路由独享
、组件级
。影响范畴由大到小,例如全局的router.beforeEach()
,能够注册一个全局前置守卫,每次路由导航都会通过这个守卫,因而在其外部能够退出管制逻辑决定用户是否能够导航到指标路由;在路由注册的时候能够退出单路由独享的守卫,例如beforeEnter
,守卫只在进入路由时触发,因而只会影响这个路由,管制更准确;咱们还能够为路由组件增加守卫配置,例如beforeRouteEnter
,会在渲染该组件的对应路由被验证前调用,管制的范畴更准确了。 - 用户的任何导航行为都会走
navigate
办法,外部有个guards
队列按程序执行用户注册的守卫钩子函数,如果没有通过验证逻辑则会勾销原有的导航。
原理
runGuardQueue(guards)
链式的执行用户在各级别注册的守卫钩子函数,通过则持续下一个级别的守卫,不通过进入 catch
流程勾销本来导航
// 源码 | |
runGuardQueue(guards) | |
.then(() => { | |
// check global guards beforeEach | |
guards = [] | |
for (const guard of beforeGuards.list()) {guards.push(guardToPromiseFn(guard, to, from)) | |
} | |
guards.push(canceledNavigationCheck) | |
return runGuardQueue(guards) | |
}) | |
.then(() => { | |
// check in components beforeRouteUpdate | |
guards = extractComponentsGuards( | |
updatingRecords, | |
'beforeRouteUpdate', | |
to, | |
from | |
) | |
for (const record of updatingRecords) { | |
record.updateGuards.forEach(guard => {guards.push(guardToPromiseFn(guard, to, from)) | |
}) | |
} | |
guards.push(canceledNavigationCheck) | |
// run the queue of per route beforeEnter guards | |
return runGuardQueue(guards) | |
}) | |
.then(() => { | |
// check the route beforeEnter | |
guards = [] | |
for (const record of to.matched) { | |
// do not trigger beforeEnter on reused views | |
if (record.beforeEnter && !from.matched.includes(record)) {if (isArray(record.beforeEnter)) {for (const beforeEnter of record.beforeEnter) | |
guards.push(guardToPromiseFn(beforeEnter, to, from)) | |
} else {guards.push(guardToPromiseFn(record.beforeEnter, to, from)) | |
} | |
} | |
} | |
guards.push(canceledNavigationCheck) | |
// run the queue of per route beforeEnter guards | |
return runGuardQueue(guards) | |
}) | |
.then(() => {// NOTE: at this point to.matched is normalized and does not contain any () => Promise<Component> | |
// clear existing enterCallbacks, these are added by extractComponentsGuards | |
to.matched.forEach(record => (record.enterCallbacks = {})) | |
// check in-component beforeRouteEnter | |
guards = extractComponentsGuards( | |
enteringRecords, | |
'beforeRouteEnter', | |
to, | |
from | |
) | |
guards.push(canceledNavigationCheck) | |
// run the queue of per route beforeEnter guards | |
return runGuardQueue(guards) | |
}) | |
.then(() => { | |
// check global guards beforeResolve | |
guards = [] | |
for (const guard of beforeResolveGuards.list()) {guards.push(guardToPromiseFn(guard, to, from)) | |
} | |
guards.push(canceledNavigationCheck) | |
return runGuardQueue(guards) | |
}) | |
// catch any navigation canceled | |
.catch(err => | |
isNavigationFailure(err, ErrorTypes.NAVIGATION_CANCELLED) | |
? err | |
: Promise.reject(err) | |
) |
源码地位(opens new window)
Vue data 中某一个属性的值产生扭转后,视图会立刻同步执行从新渲染吗?
不会立刻同步执行从新渲染。Vue 实现响应式并不是数据发生变化之后 DOM 立刻变动,而是按肯定的策略进行 DOM 的更新。Vue 在更新 DOM 时是异步执行的。只有侦听到数据变动,Vue 将开启一个队列,并缓冲在同一事件循环中产生的所有数据变更。
如果同一个 watcher 被屡次触发,只会被推入到队列中一次。这种在缓冲时去除反复数据对于防止不必要的计算和 DOM 操作是十分重要的。而后,在下一个的事件循环 tick 中,Vue 刷新队列并执行理论(已去重的)工作。
v-if 和 v -for 哪个优先级更高
- 实际中不应该把
v-for
和v-if
放一起 - 在
vue2
中,v-for
的优先级是高于v-if
,把它们放在一起,输入的渲染函数中能够看出会先执行循环再判断条件,哪怕咱们只渲染列表中一小部分元素,也得在每次重渲染的时候遍历整个列表,这会比拟节约;另外须要留神的是在vue3
中则齐全相同,v-if
的优先级高于v-for
,所以v-if
执行时,它调用的变量还不存在,就会导致异样 -
通常有两种状况下导致咱们这样做:
- 为了过滤列表中的我的项目 (比方
v-for="user in users" v-if="user.isActive"
)。此时定义一个计算属性 (比方activeUsers
),让其返回过滤后的列表即可(比方users.filter(u=>u.isActive)
) - 为了防止渲染本应该被暗藏的列表 (比方
v-for="user in users" v-if="shouldShowUsers"
)。此时把v-if
挪动至容器元素上 (比方ul
、ol
)或者外面包一层template
即可
- 为了过滤列表中的我的项目 (比方
- 文档中明确指出永远不要把
v-if
和v-for
同时用在同一个元素上,显然这是一个重要的注意事项 - 源码外面对于代码生成的局部,可能清晰的看到是先解决
v-if
还是v-for
,程序上vue2
和vue3
正好相同,因而产生了一些症状的不同,然而不管怎样都是不能把它们写在一起的
vue2.x 源码剖析
在 vue 模板编译的时候,会将指令系统转化成可执行的
render
函数
编写一个 p
标签,同时应用 v-if
与 v-for
<div id="app"> | |
<p v-if="isShow" v-for="item in items"> | |
{{item.title}} | |
</p> | |
</div> |
创立 vue
实例,寄存 isShow
与items
数据
const app = new Vue({ | |
el: "#app", | |
data() { | |
return { | |
items: [{ title: "foo"}, | |
{title: "baz"}] | |
} | |
}, | |
computed: {isShow() {return this.items && this.items.length > 0} | |
} | |
}) |
模板指令的代码都会生成在 render
函数中,通过 app.$options.render
就能失去渲染函数
ƒ anonymous() {with (this) { return | |
_c('div', { attrs: { "id": "app"} }, | |
_l((items), function (item) | |
{return (isShow) ? _c('p', [_v("\n" + _s(item.title) + "\n")]) : _e()}), 0) } | |
} |
_l
是vue
的列表渲染函数,函数外部都会进行一次if
判断- 初步失去论断:
v-for
优先级是比v-i
f 高 - 再将
v-for
与v-if
置于不同标签
<div id="app"> | |
<template v-if="isShow"> | |
<p v-for="item in items">{{item.title}}</p> | |
</template> | |
</div> |
再输入下 render
函数
ƒ anonymous() {with(this){return | |
_c('div',{attrs:{"id":"app"}}, | |
[(isShow)?[_v("\n"), | |
_l((items),function(item){return _c('p',[_v(_s(item.title))])})]:_e()],2)} | |
} |
这时候咱们能够看到,v-for
与 v-if
作用在不同标签时候,是先进行判断,再进行列表的渲染
咱们再在查看下 vue 源码
源码地位:\vue-dev\src\compiler\codegen\index.js
export function genElement (el: ASTElement, state: CodegenState): string {if (el.parent) {el.pre = el.pre || el.parent.pre} | |
if (el.staticRoot && !el.staticProcessed) {return genStatic(el, state) | |
} else if (el.once && !el.onceProcessed) {return genOnce(el, state) | |
} else if (el.for && !el.forProcessed) {return genFor(el, state) | |
} else if (el.if && !el.ifProcessed) {return genIf(el, state) | |
} else if (el.tag === 'template' && !el.slotTarget && !state.pre) {return genChildren(el, state) || 'void 0' | |
} else if (el.tag === 'slot') {return genSlot(el, state) | |
} else { | |
// component or element | |
... | |
} |
在进行 if
判断的时候,v-for
是比 v-if
先进行判断
最终论断:v-for
优先级比 v-if
高
为什么要应用异步组件
- 节俭打包出的后果,异步组件离开打包,采纳
jsonp
的形式进行加载,无效解决文件过大的问题。 - 外围就是包组件定义变成一个函数,依赖
import()
语法,能够实现文件的宰割加载。
components:{AddCustomerSchedule:(resolve)=>import("../components/AddCustomer") // require([]) | |
} |
原理
export function (Ctor: Class<Component> | Function | Object | void, data: ?VNodeData, context: Component, children: ?Array<VNode>, tag?: string): VNode | Array<VNode> | void { | |
// async component | |
let asyncFactory | |
if (isUndef(Ctor.cid)) { | |
asyncFactory = Ctor | |
Ctor = resolveAsyncComponent(asyncFactory, baseCtor) // 默认调用此函数时返回 undefiend | |
// 第二次渲染时 Ctor 不为 undefined | |
if (Ctor === undefined) { | |
return createAsyncPlaceholder( // 渲染占位符 空虚构节点 | |
asyncFactory, | |
data, | |
context, | |
children, | |
tag | |
) | |
} | |
} | |
} | |
function resolveAsyncComponent (factory: Function, baseCtor: Class<Component>): Class<Component> | void {if (isDef(factory.resolved)) { | |
// 3. 在次渲染时能够拿到获取的最新组件 | |
return factory.resolved | |
} | |
const resolve = once((res: Object | Class<Component>) => {factory.resolved = ensureCtor(res, baseCtor) | |
if (!sync) {forceRender(true) //2. 强制更新视图从新渲染 | |
} else {owners.length = 0} | |
}) | |
const reject = once(reason => {if (isDef(factory.errorComp)) {factory.error = true forceRender(true) | |
} | |
}) | |
const res = factory(resolve, reject)// 1. 将 resolve 办法和 reject 办法传入,用户调用 resolve 办法后 | |
sync = false | |
return factory.resolved | |
} |
computed 实质是一个惰性求值的观察者。
computed 外部实现了一个惰性的 watcher, 也就是 computed watcher,computed watcher 不会立即求值, 同时持有一个 dep 实例。
其外部通过 this.dirty 属性标记计算属性是否须要从新求值。
当 computed 的依赖状态产生扭转时, 就会告诉这个惰性的 watcher,
computed watcher 通过 this.dep.subs.length 判断有没有订阅者,
有的话, 会从新计算, 而后比照新旧值, 如果变动了, 会从新渲染。(Vue 想确保不仅仅是计算属性依赖的值发生变化,而是当计算属性最终计算的值发生变化时才会触发渲染 watcher 从新渲染,实质上是一种优化。)
没有的话, 仅仅把 this.dirty = true。(当计算属性依赖于其余数据时,属性并不会立刻从新计算,只有之后其余中央须要读取属性的时候,它才会真正计算,即具备 lazy(懒计算)个性。)
Vue-router 跳转和 location.href 有什么区别
- 应用
location.href= /url
来跳转,简略不便,然而刷新了页面; - 应用
history.pushState(/url)
,无刷新页面,动态跳转; - 引进 router,而后应用
router.push(/url)
来跳转,应用了diff
算法,实现了按需加载,缩小了 dom 的耗费。其实应用 router 跳转和应用history.pushState()
没什么差异的,因为 vue-router 就是用了history.pushState()
,尤其是在 history 模式下。
$nextTick 原理及作用
Vue 的 nextTick 其本质是对 JavaScript 执行原理 EventLoop 的一种利用。
nextTick 的外围是利用了如 Promise、MutationObserver、setImmediate、setTimeout 的原生 JavaScript 办法来模仿对应的微 / 宏工作的实现,实质是为了利用 JavaScript 的这些异步回调工作队列来实现 Vue 框架中本人的异步回调队列。
nextTick 不仅是 Vue 外部的异步队列的调用办法,同时也容许开发者在理论我的项目中应用这个办法来满足理论利用中对 DOM 更新数据机会的后续逻辑解决
nextTick 是典型的将底层 JavaScript 执行原理利用到具体案例中的示例,引入异步更新队列机制的起因∶
- 如果是同步更新,则屡次对一个或多个属性赋值,会频繁触发 UI/DOM 的渲染,能够缩小一些无用渲染
- 同时因为 VirtualDOM 的引入,每一次状态发生变化后,状态变动的信号会发送给组件,组件外部应用 VirtualDOM 进行计算得出须要更新的具体的 DOM 节点,而后对 DOM 进行更新操作,每次更新状态后的渲染过程须要更多的计算,而这种无用功也将节约更多的性能,所以异步渲染变得更加至关重要
Vue 采纳了数据驱动视图的思维,然而在一些状况下,依然须要操作 DOM。有时候,可能遇到这样的状况,DOM1 的数据产生了变动,而 DOM2 须要从 DOM1 中获取数据,那这时就会发现 DOM2 的视图并没有更新,这时就须要用到了 nextTick
了。
因为 Vue 的 DOM 操作是异步的,所以,在下面的状况中,就要将 DOM2 获取数据的操作写在 $nextTick
中。
this.$nextTick(() => { // 获取数据的操作...})
所以,在以下状况下,会用到 nextTick:
- 在数据变动后执行的某个操作,而这个操作须要应用随数据变动而变动的 DOM 构造的时候,这个操作就须要办法在
nextTick()
的回调函数中。 - 在 vue 生命周期中,如果在 created()钩子进行 DOM 操作,也肯定要放在
nextTick()
的回调函数中。
因为在 created()钩子函数中,页面的 DOM 还未渲染,这时候也没方法操作 DOM,所以,此时如果想要操作 DOM,必须将操作的代码放在 nextTick()
的回调函数中。
Vue3.0 和 2.0 的响应式原理区别
Vue3.x 改用 Proxy 代替 Object.defineProperty。因为 Proxy 能够间接监听对象和数组的变动,并且有多达 13 种拦挡办法。
相干代码如下
import {mutableHandlers} from "./baseHandlers"; // 代理相干逻辑 | |
import {isObject} from "./util"; // 工具办法 | |
export function reactive(target) { | |
// 依据不同参数创立不同响应式对象 | |
return createReactiveObject(target, mutableHandlers); | |
} | |
function createReactiveObject(target, baseHandler) {if (!isObject(target)) {return target;} | |
const observed = new Proxy(target, baseHandler); | |
return observed; | |
} | |
const get = createGetter(); | |
const set = createSetter(); | |
function createGetter() {return function get(target, key, receiver) { | |
// 对获取的值进行喷射 | |
const res = Reflect.get(target, key, receiver); | |
console.log("属性获取", key); | |
if (isObject(res)) { | |
// 如果获取的值是对象类型,则返回以后对象的代理对象 | |
return reactive(res); | |
} | |
return res; | |
}; | |
} | |
function createSetter() {return function set(target, key, value, receiver) {const oldValue = target[key]; | |
const hadKey = hasOwn(target, key); | |
const result = Reflect.set(target, key, value, receiver); | |
if (!hadKey) {console.log("属性新增", key, value); | |
} else if (hasChanged(value, oldValue)) {console.log("属性值被批改", key, value); | |
} | |
return result; | |
}; | |
} | |
export const mutableHandlers = { | |
get, // 当获取属性时调用此办法 | |
set, // 当批改属性时调用此办法 | |
}; |
vue3 中 watch、watchEffect 区别
watch
是惰性执行,也就是只有监听的值发生变化的时候才会执行,然而watchEffect
不同,每次代码加载watchEffect
都会执行(疏忽watch
第三个参数的配置,如果批改配置项也能够实现立刻执行)watch
须要传递监听的对象,watchEffect
不须要watch
只能监听响应式数据:ref
定义的属性和reactive
定义的对象,如果间接监听reactive
定义对象中的属性是不容许的(会报正告),除非应用函数转换一下。其实就是官网上说的监听一个getter
watchEffect
如果监听reactive
定义的对象是不起作用的,只能监听对象中的属性
看一下 watchEffect
的代码
<template> | |
<div> | |
请输出 firstName:<input type="text" v-model="firstName"> | |
</div> | |
<div> | |
请输出 lastName:<input type="text" v-model="lastName"> | |
</div> | |
<div> | |
请输出 obj.text:<input type="text" v-model="obj.text"> | |
</div> | |
<div>【obj.text】{{obj.text}} | |
</div> | |
</template> | |
<script> | |
import {ref, reactive, watch, watchEffect} from 'vue' | |
export default { | |
name: "HelloWorld", | |
props: {msg: String,}, | |
setup(props,content){let firstName = ref('') | |
let lastName = ref('') | |
let obj= reactive({text:'hello'}) | |
watchEffect(()=>{console.log('触发了 watchEffect'); | |
console.log(` 组合后的名称为:${firstName.value}${lastName.value}`) | |
}) | |
return{ | |
obj, | |
firstName, | |
lastName | |
} | |
} | |
}; | |
</script> |
革新一下代码
watchEffect(()=>{console.log('触发了 watchEffect'); | |
// 这里咱们不应用 firstName.value/lastName.value,相当于是监控整个 ref, 对应第四点下面的论断 | |
console.log(` 组合后的名称为:${firstName}${lastName}`) | |
}) |
watchEffect(()=>{console.log('触发了 watchEffect'); | |
console.log(obj); | |
}) |
略微革新一下
let obj = reactive({text:'hello'}) | |
watchEffect(()=>{console.log('触发了 watchEffect'); | |
console.log(obj.text); | |
}) |
再看一下 watch 的代码,验证一下
let obj= reactive({text:'hello'}) | |
// watch 是惰性执行,默认初始化之后不会执行,只有值有变动才会触发,可通过配置参数实现默认执行 | |
watch(obj, (newValue, oldValue) => { | |
// 回调函数 | |
console.log('触发监控更新了 new', newValue); | |
console.log('触发监控更新了 old', oldValue); | |
},{ | |
// 配置 immediate 参数,立刻执行,以及深层次监听 | |
immediate: true, | |
deep: true | |
}) |
- 监控整个
reactive
对象,从下面的图能够看到deep
理论默认是开启的,就算咱们设置为false
也还是有效。而且旧值获取不到。 - 要获取旧值则须要监控对象的属性,也就是监听一个
getter
,看下图
总结
- 如果定义了
reactive
的数据,想去应用watch
监听数据扭转,则无奈正确获取旧值,并且deep
属性配置有效,主动强制开启了深层次监听。 - 如果应用
ref
初始化一个对象或者数组类型的数据,会被主动转成reactive
的实现形式,生成proxy
代理对象。也会变得无奈正确取旧值。 - 用任何形式生成的数据,如果接管的变量是一个
proxy
代理对象,就都会导致watch
这个对象时,watch
回调里无奈正确获取旧值。 - 所以当大家应用
watch
监听对象时,如果在不须要应用旧值的状况,能够失常监听对象没关系;然而如果当监听扭转函数外面须要用到旧值时,只能监听 对象.xxx` 属性 的形式才行
watch 和 watchEffect 异同总结
体验
watchEffect
立刻运行一个函数,而后被动地追踪它的依赖,当这些依赖扭转时从新执行该函数
const count = ref(0) | |
| |
watchEffect(() => console.log(count.value)) | |
// -> logs 0 | |
| |
count.value++ | |
// -> logs 1 |
watch
侦测一个或多个响应式数据源并在数据源变动时调用一个回调函数
const state = reactive({count: 0}) | |
watch(() => state.count, | |
(count, prevCount) => {/* ... */} | |
) |
答复范例
watchEffect
立刻运行一个函数,而后被动地追踪它的依赖,当这些依赖扭转时从新执行该函数。watch
侦测一个或多个响应式数据源并在数据源变动时调用一个回调函数watchEffect(effect)
是一种非凡watch
,传入的函数既是依赖收集的数据源,也是回调函数。如果咱们不关怀响应式数据变动前后的值,只是想拿这些数据做些事件,那么watchEffect
就是咱们须要的。watch
更底层,能够接管多种数据源,包含用于依赖收集的getter
函数,因而它齐全能够实现watchEffect
的性能,同时因为能够指定getter
函数,依赖能够管制的更准确,还能获取数据变动前后的值,因而如果须要这些时咱们会应用watch
watchEffect
在应用时,传入的函数会立即执行一次。watch
默认状况下并不会执行回调函数,除非咱们手动设置immediate
选项- 从实现上来说,
watchEffect(fn)
相当于watch(fn,fn,{immediate:true})
watchEffect
定义如下
export function watchEffect( | |
effect: WatchEffect, | |
options?: WatchOptionsBase | |
): WatchStopHandle {return doWatch(effect, null, options) | |
} |
watch
定义如下
export function watch<T = any, Immediate extends Readonly<boolean> = false>( | |
source: T | WatchSource<T>, | |
cb: any, | |
options?: WatchOptions<Immediate> | |
): WatchStopHandle {return doWatch(source as any, cb, options) | |
} |
很显著 watchEffect
就是一种非凡的 watch
实现。
nextTick 应用场景和原理
nextTick 中的回调是在下次 DOM 更新循环完结之后执行的提早回调。在批改数据之后立刻应用这个办法,获取更新后的 DOM。次要思路就是采纳微工作优先的形式调用异步办法去执行 nextTick 包装的办法
相干代码如下
let callbacks = []; | |
let pending = false; | |
function flushCallbacks() { | |
pending = false; // 把标记还原为 false | |
// 顺次执行回调 | |
for (let i = 0; i < callbacks.length; i++) {callbacks[i]();} | |
} | |
let timerFunc; // 定义异步办法 采纳优雅降级 | |
if (typeof Promise !== "undefined") { | |
// 如果反对 promise | |
const p = Promise.resolve(); | |
timerFunc = () => {p.then(flushCallbacks); | |
}; | |
} else if (typeof MutationObserver !== "undefined") { | |
// MutationObserver 次要是监听 dom 变动 也是一个异步办法 | |
let counter = 1; | |
const observer = new MutationObserver(flushCallbacks); | |
const textNode = document.createTextNode(String(counter)); | |
observer.observe(textNode, {characterData: true,}); | |
timerFunc = () => {counter = (counter + 1) % 2; | |
textNode.data = String(counter); | |
}; | |
} else if (typeof setImmediate !== "undefined") { | |
// 如果后面都不反对 判断 setImmediate | |
timerFunc = () => {setImmediate(flushCallbacks); | |
}; | |
} else { | |
// 最初降级采纳 setTimeout | |
timerFunc = () => {setTimeout(flushCallbacks, 0); | |
}; | |
} | |
export function nextTick(cb) { | |
// 除了渲染 watcher 还有用户本人手动调用的 nextTick 一起被收集到数组 | |
callbacks.push(cb); | |
if (!pending) { | |
// 如果屡次调用 nextTick 只会执行一次异步 等异步队列清空之后再把标记变为 false | |
pending = true; | |
timerFunc();} | |
} |
vue2.x 具体
1. 剖析
首先找到 vue
的构造函数
源码地位:src\core\instance\index.js
function Vue (options) { | |
if (process.env.NODE_ENV !== 'production' && | |
!(this instanceof Vue) | |
) {warn('Vue is a constructor and should be called with the `new` keyword') | |
} | |
this._init(options) | |
} |
options
是用户传递过去的配置项,如 data、methods
等罕用的办法
vue
构建函数调用 _init
办法,但咱们发现本文件中并没有此办法,但认真能够看到文件下方定定义了很多初始化办法
initMixin(Vue); // 定义 _init | |
stateMixin(Vue); // 定义 $set $get $delete $watch 等 | |
eventsMixin(Vue); // 定义事件 $on $once $off $emit | |
lifecycleMixin(Vue);// 定义 _update $forceUpdate $destroy | |
renderMixin(Vue); // 定义 _render 返回虚构 dom |
首先能够看 initMixin
办法,发现该办法在 Vue
原型上定义了 _init
办法
源码地位:src\core\instance\init.js
Vue.prototype._init = function (options?: Object) { | |
const vm: Component = this | |
// a uid | |
vm._uid = uid++ | |
let startTag, endTag | |
/* istanbul ignore if */ | |
if (process.env.NODE_ENV !== 'production' && config.performance && mark) {startTag = `vue-perf-start:${vm._uid}` | |
endTag = `vue-perf-end:${vm._uid}` | |
mark(startTag) | |
} | |
// a flag to avoid this being observed | |
vm._isVue = true | |
// merge options | |
// 合并属性,判断初始化的是否是组件,这里合并次要是 mixins 或 extends 的办法 | |
if (options && options._isComponent) { | |
// optimize internal component instantiation | |
// since dynamic options merging is pretty slow, and none of the | |
// internal component options needs special treatment. | |
initInternalComponent(vm, options) | |
} else { // 合并 vue 属性 | |
vm.$options = mergeOptions(resolveConstructorOptions(vm.constructor), | |
options || {}, | |
vm | |
) | |
} | |
/* istanbul ignore else */ | |
if (process.env.NODE_ENV !== 'production') { | |
// 初始化 proxy 拦截器 | |
initProxy(vm) | |
} else {vm._renderProxy = vm} | |
// expose real self | |
vm._self = vm | |
// 初始化组件生命周期标记位 | |
initLifecycle(vm) | |
// 初始化组件事件侦听 | |
initEvents(vm) | |
// 初始化渲染办法 | |
initRender(vm) | |
callHook(vm, 'beforeCreate') | |
// 初始化依赖注入内容,在初始化 data、props 之前 | |
initInjections(vm) // resolve injections before data/props | |
// 初始化 props/data/method/watch/methods | |
initState(vm) | |
initProvide(vm) // resolve provide after data/props | |
callHook(vm, 'created') | |
/* istanbul ignore if */ | |
if (process.env.NODE_ENV !== 'production' && config.performance && mark) {vm._name = formatComponentName(vm, false) | |
mark(endTag) | |
measure(`vue ${vm._name} init`, startTag, endTag) | |
} | |
// 挂载元素 | |
if (vm.$options.el) {vm.$mount(vm.$options.el) | |
} | |
} |
仔细阅读下面的代码,咱们失去以下论断:
- 在调用
beforeCreate
之前,数据初始化并未实现,像data
、props
这些属性无法访问到 - 到了
created
的时候,数据曾经初始化实现,可能拜访data
、props
这些属性,但这时候并未实现dom
的挂载,因而无法访问到dom
元素 - 挂载办法是调用
vm.$mount
办法
initState
办法是实现 props/data/method/watch/methods
的初始化
源码地位:src\core\instance\state.js
export function initState (vm: Component) { | |
// 初始化组件的 watcher 列表 | |
vm._watchers = [] | |
const opts = vm.$options | |
// 初始化 props | |
if (opts.props) initProps(vm, opts.props) | |
// 初始化 methods 办法 | |
if (opts.methods) initMethods(vm, opts.methods) | |
if (opts.data) { | |
// 初始化 data | |
initData(vm) | |
} else {observe(vm._data = {}, true /* asRootData */) | |
} | |
if (opts.computed) initComputed(vm, opts.computed) | |
if (opts.watch && opts.watch !== nativeWatch) {initWatch(vm, opts.watch) | |
} | |
} |
咱们和这里次要看初始化 data
的办法为 initData
,它与initState
在同一文件上
function initData (vm: Component) { | |
let data = vm.$options.data | |
// 获取到组件上的 data | |
data = vm._data = typeof data === 'function' | |
? getData(data, vm) | |
: data || {} | |
if (!isPlainObject(data)) {data = {} | |
process.env.NODE_ENV !== 'production' && warn( | |
'data functions should return an object:\n' + | |
'https://vuejs.org/v2/guide/components.html#data-Must-Be-a-Function', | |
vm | |
) | |
} | |
// proxy data on instance | |
const keys = Object.keys(data) | |
const props = vm.$options.props | |
const methods = vm.$options.methods | |
let i = keys.length | |
while (i--) {const key = keys[i] | |
if (process.env.NODE_ENV !== 'production') { | |
// 属性名不能与办法名反复 | |
if (methods && hasOwn(methods, key)) { | |
warn(`Method "${key}" has already been defined as a data property.`, | |
vm | |
) | |
} | |
} | |
// 属性名不能与 state 名称反复 | |
if (props && hasOwn(props, key)) { | |
process.env.NODE_ENV !== 'production' && warn(`The data property "${key}" is already declared as a prop. ` + | |
`Use prop default value instead.`, | |
vm | |
) | |
} else if (!isReserved(key)) { // 验证 key 值的合法性 | |
// 将_data 中的数据挂载到组件 vm 上, 这样就能够通过 this.xxx 拜访到组件上的数据 | |
proxy(vm, `_data`, key) | |
} | |
} | |
// observe data | |
// 响应式监听 data 是数据的变动 | |
observe(data, true /* asRootData */) | |
} |
仔细阅读下面的代码,咱们能够失去以下论断:
- 初始化程序:
props
、methods
、data
data
定义的时候可选择函数模式或者对象模式(组件只能为函数模式)
对于数据响应式在这就不开展具体阐明
上文提到挂载办法是调用 vm.$mount
办法
源码地位:
Vue.prototype.$mount = function ( | |
el?: string | Element, | |
hydrating?: boolean | |
): Component { | |
// 获取或查问元素 | |
el = el && query(el) | |
/* istanbul ignore if */ | |
// vue 不容许间接挂载到 body 或页面文档上 | |
if (el === document.body || el === document.documentElement) { | |
process.env.NODE_ENV !== 'production' && warn(`Do not mount Vue to <html> or <body> - mount to normal elements instead.`) | |
return this | |
} | |
const options = this.$options | |
// resolve template/el and convert to render function | |
if (!options.render) { | |
let template = options.template | |
// 存在 template 模板,解析 vue 模板文件 | |
if (template) {if (typeof template === 'string') {if (template.charAt(0) === '#') {template = idToTemplate(template) | |
/* istanbul ignore if */ | |
if (process.env.NODE_ENV !== 'production' && !template) { | |
warn(`Template element not found or is empty: ${options.template}`, | |
this | |
) | |
} | |
} | |
} else if (template.nodeType) {template = template.innerHTML} else {if (process.env.NODE_ENV !== 'production') {warn('invalid template option:' + template, this) | |
} | |
return this | |
} | |
} else if (el) { | |
// 通过选择器获取元素内容 | |
template = getOuterHTML(el) | |
} | |
if (template) { | |
/* istanbul ignore if */ | |
if (process.env.NODE_ENV !== 'production' && config.performance && mark) {mark('compile') | |
} | |
/** | |
* 1. 将 temmplate 解析 ast tree | |
* 2. 将 ast tree 转换成 render 语法字符串 | |
* 3. 生成 render 办法 | |
*/ | |
const {render, staticRenderFns} = compileToFunctions(template, { | |
outputSourceRange: process.env.NODE_ENV !== 'production', | |
shouldDecodeNewlines, | |
shouldDecodeNewlinesForHref, | |
delimiters: options.delimiters, | |
comments: options.comments | |
}, this) | |
options.render = render | |
options.staticRenderFns = staticRenderFns | |
/* istanbul ignore if */ | |
if (process.env.NODE_ENV !== 'production' && config.performance && mark) {mark('compile end') | |
measure(`vue ${this._name} compile`, 'compile', 'compile end') | |
} | |
} | |
} | |
return mount.call(this, el, hydrating) | |
} |
浏览下面代码,咱们能失去以下论断:
- 不要将根元素放到
body
或者html
上 - 能够在对象中定义
template/render
或者间接应用template
、el
示意元素选择器 - 最终都会解析成
render
函数,调用compileToFunctions
,会将template
解析成render
函数
对 template
的解析步骤大抵分为以下几步:
- 将
html
文档片段解析成ast
描述符 - 将
ast
描述符解析成字符串 - 生成
render
函数
生成 render
函数,挂载到 vm
上后,会再次调用 mount
办法
源码地位:src\platforms\web\runtime\index.js
// public mount method | |
Vue.prototype.$mount = function ( | |
el?: string | Element, | |
hydrating?: boolean | |
): Component {el = el && inBrowser ? query(el) : undefined | |
// 渲染组件 | |
return mountComponent(this, el, hydrating) | |
} |
调用 mountComponent
渲染组件
export function mountComponent ( | |
vm: Component, | |
el: ?Element, | |
hydrating?: boolean | |
): Component { | |
vm.$el = el | |
// 如果没有获取解析的 render 函数,则会抛出正告 | |
// render 是解析模板文件生成的 | |
if (!vm.$options.render) { | |
vm.$options.render = createEmptyVNode | |
if (process.env.NODE_ENV !== 'production') { | |
/* istanbul ignore if */ | |
if ((vm.$options.template && vm.$options.template.charAt(0) !== '#') || | |
vm.$options.el || el) { | |
warn( | |
'You are using the runtime-only build of Vue where the template' + | |
'compiler is not available. Either pre-compile the templates into' + | |
'render functions, or use the compiler-included build.', | |
vm | |
) | |
} else { | |
// 没有获取到 vue 的模板文件 | |
warn( | |
'Failed to mount component: template or render function not defined.', | |
vm | |
) | |
} | |
} | |
} | |
// 执行 beforeMount 钩子 | |
callHook(vm, 'beforeMount') | |
let updateComponent | |
/* istanbul ignore if */ | |
if (process.env.NODE_ENV !== 'production' && config.performance && mark) {updateComponent = () => { | |
const name = vm._name | |
const id = vm._uid | |
const startTag = `vue-perf-start:${id}` | |
const endTag = `vue-perf-end:${id}` | |
mark(startTag) | |
const vnode = vm._render() | |
mark(endTag) | |
measure(`vue ${name} render`, startTag, endTag) | |
mark(startTag) | |
vm._update(vnode, hydrating) | |
mark(endTag) | |
measure(`vue ${name} patch`, startTag, endTag) | |
} | |
} else { | |
// 定义更新函数 | |
updateComponent = () => { | |
// 理论调⽤是在 lifeCycleMixin 中定义的_update 和 renderMixin 中定义的_render | |
vm._update(vm._render(), hydrating) | |
} | |
} | |
// we set this to vm._watcher inside the watcher's constructor | |
// since the watcher's initial patch may call $forceUpdate (e.g. inside child | |
// component's mounted hook), which relies on vm._watcher being already defined | |
// 监听以后组件状态,当有数据变动时,更新组件 | |
new Watcher(vm, updateComponent, noop, {before () {if (vm._isMounted && !vm._isDestroyed) { | |
// 数据更新引发的组件更新 | |
callHook(vm, 'beforeUpdate') | |
} | |
} | |
}, true /* isRenderWatcher */) | |
hydrating = false | |
// manually mounted instance, call mounted on self | |
// mounted is called for render-created child components in its inserted hook | |
if (vm.$vnode == null) { | |
vm._isMounted = true | |
callHook(vm, 'mounted') | |
} | |
return vm | |
} |
浏览下面代码,咱们失去以下论断:
- 会触发
boforeCreate
钩子 - 定义
updateComponent
渲染页面视图的办法 - 监听组件数据,一旦发生变化,触发
beforeUpdate
生命钩子
updateComponent
办法次要执行在 vue
初始化时申明的 render
,update
办法
render的作用次要是生成
vnode
源码地位:src\core\instance\render.js
// 定义 vue 原型上的 render 办法 | |
Vue.prototype._render = function (): VNode { | |
const vm: Component = this | |
// render 函数来自于组件的 option | |
const {render, _parentVnode} = vm.$options | |
if (_parentVnode) { | |
vm.$scopedSlots = normalizeScopedSlots( | |
_parentVnode.data.scopedSlots, | |
vm.$slots, | |
vm.$scopedSlots | |
) | |
} | |
// set parent vnode. this allows render functions to have access | |
// to the data on the placeholder node. | |
vm.$vnode = _parentVnode | |
// render self | |
let vnode | |
try { | |
// There's no need to maintain a stack because all render fns are called | |
// separately from one another. Nested component's render fns are called | |
// when parent component is patched. | |
currentRenderingInstance = vm | |
// 调用 render 办法,本人的独特的 render 办法,传入 createElement 参数,生成 vNode | |
vnode = render.call(vm._renderProxy, vm.$createElement) | |
} catch (e) {handleError(e, vm, `render`) | |
// return error render result, | |
// or previous vnode to prevent render error causing blank component | |
/* istanbul ignore else */ | |
if (process.env.NODE_ENV !== 'production' && vm.$options.renderError) { | |
try {vnode = vm.$options.renderError.call(vm._renderProxy, vm.$createElement, e) | |
} catch (e) {handleError(e, vm, `renderError`) | |
vnode = vm._vnode | |
} | |
} else {vnode = vm._vnode} | |
} finally {currentRenderingInstance = null} | |
// if the returned array contains only a single node, allow it | |
if (Array.isArray(vnode) && vnode.length === 1) {vnode = vnode[0] | |
} | |
// return empty vnode in case the render function errored out | |
if (!(vnode instanceof VNode)) {if (process.env.NODE_ENV !== 'production' && Array.isArray(vnode)) { | |
warn( | |
'Multiple root nodes returned from render function. Render function' + | |
'should return a single root node.', | |
vm | |
) | |
} | |
vnode = createEmptyVNode()} | |
// set parent | |
vnode.parent = _parentVnode | |
return vnode | |
} |
_update
次要性能是调用 patch
,将vnode
转换为实在DOM
,并且更新到页面中
源码地位:src\core\instance\lifecycle.js
Vue.prototype._update = function (vnode: VNode, hydrating?: boolean) { | |
const vm: Component = this | |
const prevEl = vm.$el | |
const prevVnode = vm._vnode | |
// 设置以后激活的作用域 | |
const restoreActiveInstance = setActiveInstance(vm) | |
vm._vnode = vnode | |
// Vue.prototype.__patch__ is injected in entry points | |
// based on the rendering backend used. | |
if (!prevVnode) { | |
// initial render | |
// 执行具体的挂载逻辑 | |
vm.$el = vm.__patch__(vm.$el, vnode, hydrating, false /* removeOnly */) | |
} else { | |
// updates | |
vm.$el = vm.__patch__(prevVnode, vnode) | |
} | |
restoreActiveInstance() | |
// update __vue__ reference | |
if (prevEl) {prevEl.__vue__ = null} | |
if (vm.$el) {vm.$el.__vue__ = vm} | |
// if parent is an HOC, update its $el as well | |
if (vm.$vnode && vm.$parent && vm.$vnode === vm.$parent._vnode) {vm.$parent.$el = vm.$el} | |
// updated hook is called by the scheduler to ensure that children are | |
// updated in a parent's updated hook. | |
} |
2. 论断
-
new Vue
的时候调用会调用_init
办法- 定义
$set
、$get
、$delete
、$watch
等办法 - 定义
$on
、$off
、$emit
、$off
等事件 - 定义
_update
、$forceUpdate
、$destroy
生命周期
- 定义
- 调用
$mount
进行页面的挂载 - 挂载的时候次要是通过
mountComponent
办法 - 定义
updateComponent
更新函数 - 执行
render
生成虚构DOM
_update
将虚构DOM
生成实在DOM
构造,并且渲染到页面中
created 和 mounted 的区别
- created: 在模板渲染成 html 前调用,即通常初始化某些属性值,而后再渲染成视图。
- mounted: 在模板渲染成 html 后调用,通常是初始化页面实现后,再对 html 的 dom 节点进行一些须要的操作。
Vue 中封装的数组办法有哪些,其如何实现页面更新
在 Vue 中,对响应式解决利用的是 Object.defineProperty 对数据进行拦挡,而这个办法并不能监听到数组外部变动,数组长度变动,数组的截取变动等,所以须要对这些操作进行 hack,让 Vue 能监听到其中的变动。那 Vue 是如何实现让这些数组办法实现元素的实时更新的呢,上面是 Vue 中对这些办法的封装:
// 缓存数组原型 | |
const arrayProto = Array.prototype; | |
// 实现 arrayMethods.__proto__ === Array.prototype | |
export const arrayMethods = Object.create(arrayProto); | |
// 须要进行性能拓展的办法 | |
const methodsToPatch = [ | |
"push", | |
"pop", | |
"shift", | |
"unshift", | |
"splice", | |
"sort", | |
"reverse" | |
]; | |
/** * Intercept mutating methods and emit events */ | |
methodsToPatch.forEach(function(method) { | |
// 缓存原生数组办法 | |
const original = arrayProto[method]; | |
def(arrayMethods, method, function mutator(...args) { | |
// 执行并缓存原生数组性能 | |
const result = original.apply(this, args); | |
// 响应式解决 | |
const ob = this.__ob__; | |
let inserted; | |
switch (method) { | |
// push、unshift 会新增索引,所以要手动 observer | |
case "push": | |
case "unshift": | |
inserted = args; | |
break; | |
// splice 办法,如果传入了第三个参数,也会有索引退出,也要手动 observer。case "splice": | |
inserted = args.slice(2); | |
break; | |
} | |
// | |
if (inserted) ob.observeArray(inserted);// 获取插入的值,并设置响应式监听 | |
// notify change | |
ob.dep.notify();// 告诉依赖更新 | |
// 返回原生数组办法的执行后果 | |
return result; | |
}); | |
}); |
简略来说就是,重写了数组中的那些原生办法,首先获取到这个数组的__ob__,也就是它的 Observer 对象,如果有新的值,就调用 observeArray 持续对新的值察看变动(也就是通过 target__proto__ == arrayMethods
来扭转了数组实例的型),而后手动调用 notify,告诉渲染 watcher,执行 update。
参考:前端 vue 面试题具体解答
vue 如何监听对象或者数组某个属性的变动
当在我的项目中间接设置数组的某一项的值,或者间接设置对象的某个属性值,这个时候,你会发现页面并没有更新。这是因为 Object.defineProperty()限度,监听不到变动。
解决形式:
- this.$set(你要扭转的数组 / 对象,你要扭转的地位 /key,你要改成什么 value)
this.$set(this.arr, 0, "OBKoro1"); // 扭转数组 this.$set(this.obj, "c", "OBKoro1"); // 扭转对象
- 调用以下几个数组的办法
splice()、push()、pop()、shift()、unshift()、sort()、reverse()
vue 源码里缓存了 array 的原型链,而后重写了这几个办法,触发这几个办法的时候会 observer 数据,意思是应用这些办法不必再进行额定的操作,视图主动进行更新。举荐应用 splice 办法会比拟好自定义, 因为 splice 能够在数组的任何地位进行删除 / 增加操作
vm.$set
的实现原理是:
- 如果指标是数组,间接应用数组的 splice 办法触发相应式;
- 如果指标是对象,会先判读属性是否存在、对象是否是响应式,最终如果要对属性进行响应式解决,则是通过调用 defineReactive 办法进行响应式解决(defineReactive 办法就是 Vue 在初始化对象时,给对象属性采纳 Object.defineProperty 动静增加 getter 和 setter 的性能所调用的办法)
Vue template 到 render 的过程
vue 的模版编译过程次要如下:template -> ast -> render 函数
vue 在模版编译版本的码中会执行 compileToFunctions 将 template 转化为 render 函数:
// 将模板编译为 render 函数 const {render, staticRenderFns} = compileToFunctions(template,options// 省略}, this)
CompileToFunctions 中的次要逻辑如下∶ (1)调用 parse 办法将 template 转化为 ast(形象语法树)
constast = parse(template.trim(), options)
- parse 的指标:把 tamplate 转换为 AST 树,它是一种用 JavaScript 对象的模式来形容整个模板。
- 解析过程:利用正则表达式程序解析模板,当解析到开始标签、闭合标签、文本的时候都会别离执行对应的 回调函数,来达到结构 AST 树的目标。
AST 元素节点总共三种类型:type 为 1 示意一般元素、2 为表达式、3 为纯文本
(2)对动态节点做优化
optimize(ast,options)
这个过程次要剖析出哪些是动态节点,给其打一个标记,为后续更新渲染能够间接跳过动态节点做优化
深度遍历 AST,查看每个子树的节点元素是否为动态节点或者动态节点根。如果为动态节点,他们生成的 DOM 永远不会扭转,这对运行时模板更新起到了极大的优化作用。
(3)生成代码
const code = generate(ast, options)
generate 将 ast 形象语法树编译成 render 字符串并将动态局部放到 staticRenderFns 中,最初通过 new Function(
` render`)
生成 render 函数。
Proxy 与 Object.defineProperty 优劣比照
Proxy 的劣势如下:
- Proxy 能够间接监听对象而非属性;
- Proxy 能够间接监听数组的变动;
- Proxy 有多达 13 种拦挡办法, 不限于 apply、ownKeys、deleteProperty、has 等等是 Object.defineProperty 不具备的;
- Proxy 返回的是一个新对象, 咱们能够只操作新的对象达到目标, 而 Object.defineProperty 只能遍历对象属性间接批改;
Proxy 作为新规范将受到浏览器厂商重点继续的性能优化,也就是传说中的新规范的性能红利;
Object.defineProperty 的劣势如下:
- 兼容性好,反对 IE9,而 Proxy 的存在浏览器兼容性问题, 而且无奈用 polyfill 磨平,因而 Vue 的作者才申明须要等到下个大版本 (3.0) 能力用 Proxy 重写。
v-show 与 v-if 有什么区别?
v-if 是 真正 的条件渲染,因为它会确保在切换过程中条件块内的事件监听器和子组件适当地被销毁和重建;也是 惰性的:如果在初始渲染时条件为假,则什么也不做——直到条件第一次变为真时,才会开始渲染条件块。
v-show 就简略得多——不论初始条件是什么,元素总是会被渲染,并且只是简略地基于 CSS 的“display”属性进行切换。
所以,v-if 实用于在运行时很少扭转条件,不须要频繁切换条件的场景;v-show 则实用于须要十分频繁切换条件的场景。
谈谈 Vue 和 React 组件化的思维
- 1. 咱们在各个页面开发的时候,会产生很多反复的性能,比方 element 中的 xxxx。像这种纯正非页面的 UI,便成为咱们罕用的 UI 组件,最后的前端组件也就仅仅指的是 UI 组件
- 2. 随着业务逻辑变得越来多是,咱们就想要咱们的组件能够解决很多事,这就是咱们常说的组件化,这个组件就不是 UI 组件了,而是包具体业务的业务组件
- 3. 这种开发思维就是分而治之。最大水平的升高开发难度和保护老本的成果。并且能够多人合作,每个人写不同的组件,最初像撘积木一样的把它形成一个页面
Vue 中 computed 和 watch 有什么区别?
计算属性 computed:
(1)** 反对缓存 **,只有依赖数据发生变化时,才会从新进行计算函数;(2)计算属性内 ** 不反对异步操作 **;(3)计算属性的函数中 ** 都有一个 get**(默认具备,获取计算属性)** 和 set**(手动增加,设置计算属性)办法;(4)计算属性是主动监听依赖值的变动,从而动静返回内容。
侦听属性 watch:
(1)** 不反对缓存 **,只有数据发生变化,就会执行侦听函数;(2)侦听属性内 ** 反对异步操作 **;(3)侦听属性的值 ** 能够是一个对象,接管 handler 回调,deep,immediate 三个属性 **;(3)监听是一个过程,在监听的值变动时,能够触发一个回调,并 ** 做一些其余事件 **。
Watch 中的 deep:true 是如何实现的
当用户指定了
watch
中的 deep 属性为true
时,如果以后监控的值是数组类型。会对对象中的每一项进行求值,此时会将以后watcher
存入到对应属性的依赖中,这样数组中对象发生变化时也会告诉数据更新
源码相干
get () {pushTarget(this) // 先将以后依赖放到 Dep.target 上 | |
let value | |
const vm = this.vm | |
try {value = this.getter.call(vm, vm) | |
} catch (e) {if (this.user) {handleError(e, vm, `getter for watcher "${this.expression}"`) | |
} else {throw e} | |
} finally {if (this.deep) { // 如果须要深度监控 | |
traverse(value) // 会对对象中的每一项取值, 取值时会执行对应的 get 办法 | |
}popTarget()} |
vue-router 中如何爱护路由
剖析
路由爱护在利用开发过程中十分重要,简直每个利用都要做各种路由权限治理,因而相当考查使用者基本功。
体验
全局守卫:
const router = createRouter({...}) | |
| |
router.beforeEach((to, from) => { | |
// ... | |
// 返回 false 以勾销导航 | |
return false | |
}) |
路由独享守卫:
const routes = [ | |
{ | |
path: '/users/:id', | |
component: UserDetails, | |
beforeEnter: (to, from) => { | |
// reject the navigation | |
return false | |
}, | |
}, | |
] |
组件内的守卫:
const UserDetails = { | |
template: `...`, | |
beforeRouteEnter(to, from) {// 在渲染该组件的对应路由被验证前调用}, | |
beforeRouteUpdate(to, from) {// 在以后路由扭转,然而该组件被复用时调用}, | |
beforeRouteLeave(to, from) {// 在导航来到渲染该组件的对应路由时调用}, | |
} |
答复
vue-router
中爱护路由的办法叫做路由守卫,次要用来通过跳转或勾销的形式守卫导航。- 路由守卫有三个级别:
全局
、路由独享
、组件级
。影响范畴由大到小,例如全局的router.beforeEach()
,能够注册一个全局前置守卫,每次路由导航都会通过这个守卫,因而在其外部能够退出管制逻辑决定用户是否能够导航到指标路由;在路由注册的时候能够退出单路由独享的守卫,例如beforeEnter
,守卫只在进入路由时触发,因而只会影响这个路由,管制更准确;咱们还能够为路由组件增加守卫配置,例如beforeRouteEnter
,会在渲染该组件的对应路由被验证前调用,管制的范畴更准确了。 - 用户的任何导航行为都会走
navigate
办法,外部有个guards
队列按程序执行用户注册的守卫钩子函数,如果没有通过验证逻辑则会勾销原有的导航。
原理
runGuardQueue(guards)
链式的执行用户在各级别注册的守卫钩子函数,通过则持续下一个级别的守卫,不通过进入 catch
流程勾销本来导航
// 源码 | |
runGuardQueue(guards) | |
.then(() => { | |
// check global guards beforeEach | |
guards = [] | |
for (const guard of beforeGuards.list()) {guards.push(guardToPromiseFn(guard, to, from)) | |
} | |
guards.push(canceledNavigationCheck) | |
return runGuardQueue(guards) | |
}) | |
.then(() => { | |
// check in components beforeRouteUpdate | |
guards = extractComponentsGuards( | |
updatingRecords, | |
'beforeRouteUpdate', | |
to, | |
from | |
) | |
for (const record of updatingRecords) { | |
record.updateGuards.forEach(guard => {guards.push(guardToPromiseFn(guard, to, from)) | |
}) | |
} | |
guards.push(canceledNavigationCheck) | |
// run the queue of per route beforeEnter guards | |
return runGuardQueue(guards) | |
}) | |
.then(() => { | |
// check the route beforeEnter | |
guards = [] | |
for (const record of to.matched) { | |
// do not trigger beforeEnter on reused views | |
if (record.beforeEnter && !from.matched.includes(record)) {if (isArray(record.beforeEnter)) {for (const beforeEnter of record.beforeEnter) | |
guards.push(guardToPromiseFn(beforeEnter, to, from)) | |
} else {guards.push(guardToPromiseFn(record.beforeEnter, to, from)) | |
} | |
} | |
} | |
guards.push(canceledNavigationCheck) | |
// run the queue of per route beforeEnter guards | |
return runGuardQueue(guards) | |
}) | |
.then(() => {// NOTE: at this point to.matched is normalized and does not contain any () => Promise<Component> | |
// clear existing enterCallbacks, these are added by extractComponentsGuards | |
to.matched.forEach(record => (record.enterCallbacks = {})) | |
// check in-component beforeRouteEnter | |
guards = extractComponentsGuards( | |
enteringRecords, | |
'beforeRouteEnter', | |
to, | |
from | |
) | |
guards.push(canceledNavigationCheck) | |
// run the queue of per route beforeEnter guards | |
return runGuardQueue(guards) | |
}) | |
.then(() => { | |
// check global guards beforeResolve | |
guards = [] | |
for (const guard of beforeResolveGuards.list()) {guards.push(guardToPromiseFn(guard, to, from)) | |
} | |
guards.push(canceledNavigationCheck) | |
return runGuardQueue(guards) | |
}) | |
// catch any navigation canceled | |
.catch(err => | |
isNavigationFailure(err, ErrorTypes.NAVIGATION_CANCELLED) | |
? err | |
: Promise.reject(err) | |
) |
源码地位(opens new window)
Vue data 中某一个属性的值产生扭转后,视图会立刻同步执行从新渲染吗?
不会立刻同步执行从新渲染。Vue 实现响应式并不是数据发生变化之后 DOM 立刻变动,而是按肯定的策略进行 DOM 的更新。Vue 在更新 DOM 时是异步执行的。只有侦听到数据变动,Vue 将开启一个队列,并缓冲在同一事件循环中产生的所有数据变更。
如果同一个 watcher 被屡次触发,只会被推入到队列中一次。这种在缓冲时去除反复数据对于防止不必要的计算和 DOM 操作是十分重要的。而后,在下一个的事件循环 tick 中,Vue 刷新队列并执行理论(已去重的)工作。
v-if 和 v -for 哪个优先级更高
- 实际中不应该把
v-for
和v-if
放一起 - 在
vue2
中,v-for
的优先级是高于v-if
,把它们放在一起,输入的渲染函数中能够看出会先执行循环再判断条件,哪怕咱们只渲染列表中一小部分元素,也得在每次重渲染的时候遍历整个列表,这会比拟节约;另外须要留神的是在vue3
中则齐全相同,v-if
的优先级高于v-for
,所以v-if
执行时,它调用的变量还不存在,就会导致异样 -
通常有两种状况下导致咱们这样做:
- 为了过滤列表中的我的项目 (比方
v-for="user in users" v-if="user.isActive"
)。此时定义一个计算属性 (比方activeUsers
),让其返回过滤后的列表即可(比方users.filter(u=>u.isActive)
) - 为了防止渲染本应该被暗藏的列表 (比方
v-for="user in users" v-if="shouldShowUsers"
)。此时把v-if
挪动至容器元素上 (比方ul
、ol
)或者外面包一层template
即可
- 为了过滤列表中的我的项目 (比方
- 文档中明确指出永远不要把
v-if
和v-for
同时用在同一个元素上,显然这是一个重要的注意事项 - 源码外面对于代码生成的局部,可能清晰的看到是先解决
v-if
还是v-for
,程序上vue2
和vue3
正好相同,因而产生了一些症状的不同,然而不管怎样都是不能把它们写在一起的
vue2.x 源码剖析
在 vue 模板编译的时候,会将指令系统转化成可执行的
render
函数
编写一个 p
标签,同时应用 v-if
与 v-for
<div id="app"> | |
<p v-if="isShow" v-for="item in items"> | |
{{item.title}} | |
</p> | |
</div> |
创立 vue
实例,寄存 isShow
与items
数据
const app = new Vue({ | |
el: "#app", | |
data() { | |
return { | |
items: [{ title: "foo"}, | |
{title: "baz"}] | |
} | |
}, | |
computed: {isShow() {return this.items && this.items.length > 0} | |
} | |
}) |
模板指令的代码都会生成在 render
函数中,通过 app.$options.render
就能失去渲染函数
ƒ anonymous() {with (this) { return | |
_c('div', { attrs: { "id": "app"} }, | |
_l((items), function (item) | |
{return (isShow) ? _c('p', [_v("\n" + _s(item.title) + "\n")]) : _e()}), 0) } | |
} |
_l
是vue
的列表渲染函数,函数外部都会进行一次if
判断- 初步失去论断:
v-for
优先级是比v-i
f 高 - 再将
v-for
与v-if
置于不同标签
<div id="app"> | |
<template v-if="isShow"> | |
<p v-for="item in items">{{item.title}}</p> | |
</template> | |
</div> |
再输入下 render
函数
ƒ anonymous() {with(this){return | |
_c('div',{attrs:{"id":"app"}}, | |
[(isShow)?[_v("\n"), | |
_l((items),function(item){return _c('p',[_v(_s(item.title))])})]:_e()],2)} | |
} |
这时候咱们能够看到,v-for
与 v-if
作用在不同标签时候,是先进行判断,再进行列表的渲染
咱们再在查看下 vue 源码
源码地位:\vue-dev\src\compiler\codegen\index.js
export function genElement (el: ASTElement, state: CodegenState): string {if (el.parent) {el.pre = el.pre || el.parent.pre} | |
if (el.staticRoot && !el.staticProcessed) {return genStatic(el, state) | |
} else if (el.once && !el.onceProcessed) {return genOnce(el, state) | |
} else if (el.for && !el.forProcessed) {return genFor(el, state) | |
} else if (el.if && !el.ifProcessed) {return genIf(el, state) | |
} else if (el.tag === 'template' && !el.slotTarget && !state.pre) {return genChildren(el, state) || 'void 0' | |
} else if (el.tag === 'slot') {return genSlot(el, state) | |
} else { | |
// component or element | |
... | |
} |
在进行 if
判断的时候,v-for
是比 v-if
先进行判断
最终论断:v-for
优先级比 v-if
高
为什么要应用异步组件
- 节俭打包出的后果,异步组件离开打包,采纳
jsonp
的形式进行加载,无效解决文件过大的问题。 - 外围就是包组件定义变成一个函数,依赖
import()
语法,能够实现文件的宰割加载。
components:{AddCustomerSchedule:(resolve)=>import("../components/AddCustomer") // require([]) | |
} |
原理
export function (Ctor: Class<Component> | Function | Object | void, data: ?VNodeData, context: Component, children: ?Array<VNode>, tag?: string): VNode | Array<VNode> | void { | |
// async component | |
let asyncFactory | |
if (isUndef(Ctor.cid)) { | |
asyncFactory = Ctor | |
Ctor = resolveAsyncComponent(asyncFactory, baseCtor) // 默认调用此函数时返回 undefiend | |
// 第二次渲染时 Ctor 不为 undefined | |
if (Ctor === undefined) { | |
return createAsyncPlaceholder( // 渲染占位符 空虚构节点 | |
asyncFactory, | |
data, | |
context, | |
children, | |
tag | |
) | |
} | |
} | |
} | |
function resolveAsyncComponent (factory: Function, baseCtor: Class<Component>): Class<Component> | void {if (isDef(factory.resolved)) { | |
// 3. 在次渲染时能够拿到获取的最新组件 | |
return factory.resolved | |
} | |
const resolve = once((res: Object | Class<Component>) => {factory.resolved = ensureCtor(res, baseCtor) | |
if (!sync) {forceRender(true) //2. 强制更新视图从新渲染 | |
} else {owners.length = 0} | |
}) | |
const reject = once(reason => {if (isDef(factory.errorComp)) {factory.error = true forceRender(true) | |
} | |
}) | |
const res = factory(resolve, reject)// 1. 将 resolve 办法和 reject 办法传入,用户调用 resolve 办法后 | |
sync = false | |
return factory.resolved | |
} |