We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
在 watch 的使用中,存在深度监听的场景,如:
data = { name: { firstName: 'xxx'} } watch: { 'name.firstName': function() {} }
Vue 是如何通过传入的 name.firstName 找到对应的值的呢?
name.firstName
/** 原理 */ function getValue(data, path) { let paths = path.split('.') let res = data while (prop = paths.shift()) { res = res[prop] } return res } const data = { a: { b: { c: 'zzz' } } } // getValue(data, 'a.b.c') console.log('getValue', getValue(data, 'a.b.c')); /** 升级版,柯里化--使用闭包方式提升性能 */ function createGetValue(path) { let paths = path.split('.') return function getValue(data) { let res = data while (prop = paths.shift()) { res = res[prop] } return res } } const data2 = { a: { b: { c: 'zzz' } } } const fn = createGetValue('a.b.c') const res = fn(data2) console.log('createGetValue', res);
The text was updated successfully, but these errors were encountered:
No branches or pull requests
在 watch 的使用中,存在深度监听的场景,如:
Vue 是如何通过传入的
name.firstName
找到对应的值的呢?The text was updated successfully, but these errors were encountered: