fix(watch): this.$watch should support watching keypath
This commit is contained in:
@@ -334,11 +334,24 @@ export function instanceWatch(
|
||||
): WatchStopHandle {
|
||||
const publicThis = this.proxy as any
|
||||
const getter = isString(source)
|
||||
? () => publicThis[source]
|
||||
? source.includes('.')
|
||||
? createPathGetter(publicThis, source)
|
||||
: () => publicThis[source]
|
||||
: source.bind(publicThis)
|
||||
return doWatch(getter, cb.bind(publicThis), options, this)
|
||||
}
|
||||
|
||||
export function createPathGetter(ctx: any, path: string) {
|
||||
const segments = path.split('.')
|
||||
return () => {
|
||||
let cur = ctx
|
||||
for (let i = 0; i < segments.length && cur; i++) {
|
||||
cur = cur[segments[i]]
|
||||
}
|
||||
return cur
|
||||
}
|
||||
}
|
||||
|
||||
function traverse(value: unknown, seen: Set<unknown> = new Set()) {
|
||||
if (!isObject(value) || seen.has(value)) {
|
||||
return value
|
||||
|
||||
@@ -20,7 +20,12 @@ import {
|
||||
isPromise
|
||||
} from '@vue/shared'
|
||||
import { computed } from './apiComputed'
|
||||
import { watch, WatchOptions, WatchCallback } from './apiWatch'
|
||||
import {
|
||||
watch,
|
||||
WatchOptions,
|
||||
WatchCallback,
|
||||
createPathGetter
|
||||
} from './apiWatch'
|
||||
import { provide, inject } from './apiInject'
|
||||
import {
|
||||
onBeforeMount,
|
||||
@@ -939,17 +944,6 @@ function createWatcher(
|
||||
}
|
||||
}
|
||||
|
||||
function createPathGetter(ctx: any, path: string) {
|
||||
const segments = path.split('.')
|
||||
return () => {
|
||||
let cur = ctx
|
||||
for (let i = 0; i < segments.length && cur; i++) {
|
||||
cur = cur[segments[i]]
|
||||
}
|
||||
return cur
|
||||
}
|
||||
}
|
||||
|
||||
export function resolveMergedOptions(
|
||||
instance: ComponentInternalInstance
|
||||
): ComponentOptions {
|
||||
|
||||
Reference in New Issue
Block a user