-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
New issue
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
Throw error/warn if watch path doesn't resolve to an existing property #11113
Comments
Wouldn't it be better if watcher run when the property becomes available? Not sure if it's doable though. |
I think it's better to do this manually, in the handler. Another possible option is optional chaining in watch path expression (like this |
…11113) Warn when: 1. A watcher is created for an undefined field. AND 2. The env is not production.
@posva I have a PR for this but I'm sort of weary of adding it to the codebase because there's already a solution for watching non-existent properties using https://github.com/vuejs/vue/blob/dev/test/unit/modules/observer/watcher.spec.js#L36 |
In other words, for a lot of users this is going to produce a warning for expected behavior. |
@despreston yeah, the warning would only make sense if it's referring to a property of a non declared object (like the jsfiddle). Maybe this is indeed way too far for a warning |
@posva even for a non-declared object, you can create an object with Vue.set(this, 'completelyNew') So you could have watchers for 'completelyNew' even before it exists. |
It doesn't seemed to work when I tried |
hm. ok. You're right, and the docs can back that up: https://vuejs.org/v2/guide/reactivity.html#Change-Detection-Caveats |
What problem does this feature solve?
Currently, if a watch path does not address an existing property, its handler would just silently never execute.
This may cause bugs due to a typo or refactoring that involves restructuring the app model. Especially if we want to watch something like
'$store.state.user.user.subscriptions'
.Here's a JSFiddle to play with: https://jsfiddle.net/WofWca/50feyxn7
What does the proposed API look like?
Just throw an error (or warn?) in such case. Like it would happen if we used
this.$watch(() => this.$store.state.user.user.subscriptions, () => { .... });
The text was updated successfully, but these errors were encountered: