vue中computed/method/watch的区别
摘要:本文通过官方文档结合源码来分析computed/method/watch的区别。
Tips:本文分析的源码版本是v2.6.11,文章中牵涉到vue响应式系统原理部分,如果不是很了解,建议先阅读上一篇文章《深入解析vue响应式原理》。
computed
首先来看官网的解释:计算属性是基于响应式依赖进行缓存的,只在相关响应式依赖发生改变时它们才会重新求值。
下面通过源码来分析computed是怎么实现响应式缓存的:
initComputed
function initComputed (vm: Component, computed: Object) {
// $flow-disable-line
const watchers = vm._computedWatchers = Object.create(null)
// computed properties are just getters during SSR
const isSSR = isServerRendering()
for (const key in computed) {
const userDef = computed[key]
const getter = typeof userDef === 'function' ? userDef : userDef.get
if (process.env.NODE_ENV !== 'production' && getter == null) {
warn(
`Getter is missing for computed property "${key}".`,
vm
)
}
if (!isSSR) {
// create internal watcher for the computed property.
watchers[key] = new Watcher(
vm,
getter || noop,
noop,
computedWatcherOptions
)
}
// component-defined computed properties are already defined on the
// component prototype. We only need to define computed properties defined
// at instantiation here.
if (!(key in vm)) {
defineComputed(vm, key, userDef)
} else if (process.env.NODE_ENV !== 'production') {
if (key in vm.$data) {
warn(`The computed property "${key}" is already defined in data.`, vm)
} else if (vm.$options.props && key in vm.$options.props) {
warn(`The computed property "${key}" is already defined as a prop.`, vm)
}
}
}
}
- 首先创建一个computedWatchers挂到vm上;
- 遍历computed属性,依次将单个computed属性的get方法作为参数创建Watcher实例保存到computedWatchers中;
- 再将单个computed属性作为参数传入defineComputed方法。
defineComputed
export function defineComputed (
target: any,
key: string,
userDef: Object | Function
) {
const shouldCache = !isServerRendering()
if (typeof userDef === 'function') {
sharedPropertyDefinition.get = shouldCache
? createComputedGetter(key)
: createGetterInvoker(userDef)
sharedPropertyDefinition.set = noop
} else {
sharedPropertyDefinition.get = userDef.get
? shouldCache && userDef.cache !== false
? createComputedGetter(key)
: createGetterInvoker(userDef.get)
: noop
sharedPropertyDefinition.set = userDef.set || noop
}
if (process.env.NODE_ENV !== 'production' &&
sharedPropertyDefinition.set === noop) {
sharedPropertyDefinition.set = function () {
warn(
`Computed property "${key}" was assigned to but it has no setter.`,
this
)
}
}
Object.defineProperty(target, key, sharedPropertyDefinition)
}
function createComputedGetter (key) {
return function computedGetter () {
const watcher = this._computedWatchers && this._computedWatchers[key]
if (watcher) {
if (watcher.dirty) {
watcher.evaluate()
}
if (Dep.target) {
watcher.depend()
}
return watcher.value
}
}
}
function createGetterInvoker(fn) {
return function computedGetter () {
return fn.call(this, this)
}
}
将计算属性挂到vm上,定义getter属性方法,方法会执行计算属性获取新值(新值会存储到Watcher.value中)及收集依赖该computed属性的视图。
总结:
- 页面初始渲染时,读取computed属性值,computed属性值的getter函数读取data数据,触发data的getter方法,将computed属性对应的Watcher绑定到data的依赖收集器Dep中;
- computed属性getter方法中,还会调用Watcher.depend方法,将上层视图的观察者也添加到data的依赖收集器Dep中;
- data属性值变更后,将会调用Dep.notify方法,通知所有依赖的Watcher进行update方法;
- 首先触发computed关联Watcher的update方法,由于lazy为true,将会设置dirty为true,表示computed属性依赖的data值已经变更,但不会调用Watcher的get方法获取新值。
- 然后触发视图关联Watcher的update方法,在更新页面时会调用computed属性值,触发定义的getter函数。由于当前dirty为true,会执行关联Watcher.get方法获取新值,更新Watcher.value的值,并返回新值,完成页面的重新渲染。
method
function initMethods (vm: Component, methods: Object) {
const props = vm.$options.props
for (const key in methods) {
if (process.env.NODE_ENV !== 'production') {
if (typeof methods[key] !== 'function') {
warn(
`Method "${key}" has type "${typeof methods[key]}" in the component definition. ` +
`Did you reference the function correctly?`,
vm
)
}
if (props && hasOwn(props, key)) {
warn(
`Method "${key}" has already been defined as a prop.`,
vm
)
}
if ((key in vm) && isReserved(key)) {
warn(
`Method "${key}" conflicts with an existing Vue instance method. ` +
`Avoid defining component methods that start with _ or $.`
)
}
}
vm[key] = typeof methods[key] !== 'function' ? noop : bind(methods[key], vm)
}
}
method的源码很简单,只是绑定了所有method的this为vm。
watch
function initWatch (vm: Component, watch: Object) {
for (const key in watch) {
const handler = watch[key]
if (Array.isArray(handler)) {
for (let i = 0; i < handler.length; i++) {
createWatcher(vm, key, handler[i])
}
} else {
createWatcher(vm, key, handler)
}
}
}
function createWatcher (
vm: Component,
expOrFn: string | Function,
handler: any,
options?: Object
) {
if (isPlainObject(handler)) {
options = handler
handler = handler.handler
}
if (typeof handler === 'string') {
handler = vm[handler]
}
return vm.$watch(expOrFn, handler, options)
}
Vue.prototype.$watch = function (
expOrFn: string | Function,
cb: any,
options?: Object
): Function {
const vm: Component = this
if (isPlainObject(cb)) {
return createWatcher(vm, expOrFn, cb, options)
}
options = options || {}
options.user = true
const watcher = new Watcher(vm, expOrFn, cb, options)
if (options.immediate) {
try {
cb.call(vm, watcher.value)
} catch (error) {
handleError(error, vm, `callback for immediate watcher "${watcher.expression}"`)
}
}
return function unwatchFn () {
watcher.teardown()
}
}
Tips:Watcher对象源码在上文中《深入解析vue响应式原理》分析过,这里就不再赘述。
经过分析watch的源码可以发现,实际上每一个watch属性对应生成了一个Watcher对象,通过获取data属性值将Watcher添加到依赖收集器Dep中,当data数据更新时,就会调用Dep.notify通知Watcher。
总结:
- computed属性是根据依赖的data属性(有可能多个)进行更新标记,等视图获取该computed属性数据时才执行更新计算,并将值缓存到对应的Watcher实例中。且只有当data数据变化后,视图读取关联computed属性才会重新计算结果。
- method属性是根据视图需要即时计算获得,不具有缓存性质,当关联data数据没有更新时也会重新计算。
- watch属性是有且只能依赖单个data属性,当data数据变化后,会立即触发Watcher.update,调用对应watch定义的方法执行。不具有缓存性质。
vue中computed/method/watch的区别的更多相关文章
- Vue中computed和watch的区别
在vue中computed和watch的真正区别是:computed产生于它的依赖,而watch产生于它的依赖的变化.只要依赖存在,我们就能访问到其对应的computed属性:但只有依赖发生了改变,我 ...
- vue中computed和watch的区别,以及适用场景
computed:通过属性计算而得来的属性 1.computed内部的函数在调用时不加(). 2.computed是依赖vm中data的属性变化而变化的,也就是说,当data中的属性发生改变的时候,当 ...
- Vue中 computed 和 methods的区别
涉及到计算部分的时候,计算属性是基于它们的依赖进行缓存的,如果说值不变,那么它就不会去重新执行,只有当值发生了改变,它才会去重新执行一次,其它时候它都是缓存的.而方法则会反复计算处理.二者之间的差距就 ...
- vue中computed(计算属性)和watch在实现父子组件props同步时的实际区分
vue中computed和watch的对比是一个很有意思的话题. 看过官网教程以后,我们往往更倾向多使用computed.computed优点很多,却在某些时候不太适用. 今天我们就稍微讨论一下,当我 ...
- vue中assets和static的区别
Vue中assets和static的区别 再一次框架定型中,与同事在静态资源的存放上有了一些分歧,后来经过查阅总结如下: 相同点: assets和static两个都是存放静态资源文件.项目中所需要 ...
- Vue中computed分析
Vue中computed分析 在Vue中computed是计算属性,其会根据所依赖的数据动态显示新的计算结果,虽然使用{{}}模板内的表达式非常便利,但是设计它们的初衷是用于简单运算的,在模板中放入太 ...
- vue中computed的作用以及用法
在vue中computed是计算属性,主要作用是把数据存储到内存中,减少不必要的请求,还可以利用computed给子组件的data赋值. 参考地址:https://www.jianshu.com/p/ ...
- Vue.js中 computed 和 methods 的区别
官方文档中已经有对其的解释了,在这里把我的理解记录一下Vue中的methods.watch.computed computed 的使用场景 HTML模板中的复杂逻辑表达式,为了防止逻辑过重导致不易维护 ...
- Vue中computed的本质及与methods的区别
一.computed的本质? computed为什么不像methods一样加小括号使用? 正常使用computed方式 运行结果 至于为什么computed为什么不像methods一样使用小括号调用, ...
随机推荐
- AD PCB模块复用
该文档为原创,转发需注明出处!https://www.cnblogs.com/brianblog/ 在画图的时候如果遇到PCB中有多个模块原理图是一模一样的时候,我们便会想能不能偷点懒,只画一个模块, ...
- Spring Cloud Alibaba基础教程-Nacos(一)
2019快结束,也有很久没写博客了,今天我们来谈谈Nacos,如果对您有帮助,麻烦左上角点个关注 ,谢谢 ! 嘻嘻 今天先写第一篇 文章目录 为什么要使用Nacos Eureka 闭源 Nacos的优 ...
- js中Tabs插件打开的标签页过多自动关闭
js方法 function addTab(ResourceID, ResourceName, Url) { if (Url != "" && Url != null ...
- Cannot assign requested address 和 SO_REUSEADDR 参数
1. 错误 今天项目中出现了大量的java.net.ConnectException: Cannot assign requested address (connect failed) 错误. 刚开始 ...
- Idea利用maven创建web项目,并配置tomcat
参考http://blog.csdn.net/myarrow/article/details/50824793#comments
- 使用Android Studio来阅读Android源码
在编译android系统后,执行下面命令来生成索引. mmm development/tools/idegen/mv ./out/target/product/tiny4412/obj/GYP/sha ...
- Java Tree 树 数据结构
说到树结构就不得不回顾 链表结构 https://www.cnblogs.com/easyidea/p/13371863.html 如果链表结构中再多一个指针会是什么情况? 是不是像树根一样,这就是 ...
- mysql基础之double,float长度标度定义
MySQL类型float double decimal的区别 float数值类型用于表示单精度浮点数值,而double数值类型用于表示双精度浮点数值,float和double都是浮点型,而decima ...
- Salesforce 大数据量处理篇(一)Skinny Table
本篇参考:https://developer.salesforce.com/docs/atlas.en-us.salesforce_large_data_volumes_bp.meta/salesfo ...
- 数据库(MySQL)最新版8.0安装教程,小白都能学会安装
首先打开数据库官网 接下来点击不用登录注册 下载好软件,双击运行程序(中间不需要点击其他,等他运行好) 点击安装服务端 ,然后点击下一步 选择自己安装目录(一定要牢记)这里我选择默认目录,点击下一步 ...