Skip to content
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

Open
WofWca opened this issue Feb 17, 2020 · 8 comments
Open

Throw error/warn if watch path doesn't resolve to an existing property #11113

WofWca opened this issue Feb 17, 2020 · 8 comments
Labels
feature request warnings related to development warnings

Comments

@WofWca
Copy link

WofWca commented Feb 17, 2020

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, () => { .... });

@posva posva added feature request warnings related to development warnings labels Feb 17, 2020
@jacekkarczmarczyk
Copy link

jacekkarczmarczyk commented Feb 17, 2020

Wouldn't it be better if watcher run when the property becomes available? Not sure if it's doable though.

@WofWca
Copy link
Author

WofWca commented Feb 17, 2020

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 $store.state.user.user?.subscriptions, with a question mark), but I feel like this would need a separate issue.

despreston added a commit to despreston/vue that referenced this issue Feb 20, 2020
…11113)

Warn when:

1. A watcher is created for an undefined field.

AND

2. The env is not production.
@despreston
Copy link
Contributor

@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 Vue.set. You can see a test case for it here:

https://github.com/vuejs/vue/blob/dev/test/unit/modules/observer/watcher.spec.js#L36

@despreston
Copy link
Contributor

In other words, for a lot of users this is going to produce a warning for expected behavior.

@posva
Copy link
Member

posva commented Feb 20, 2020

@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

@despreston
Copy link
Contributor

@posva even for a non-declared object, you can create an object with Vue.set right?

Vue.set(this, 'completelyNew')

So you could have watchers for 'completelyNew' even before it exists.

@posva
Copy link
Member

posva commented Feb 22, 2020

It doesn't seemed to work when I tried

@despreston
Copy link
Contributor

hm. ok. You're right, and the docs can back that up: https://vuejs.org/v2/guide/reactivity.html#Change-Detection-Caveats

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request warnings related to development warnings
Projects
None yet
Development

No branches or pull requests

4 participants