Skip to content

Improve error reporting/debugging of provide/inject, or allow require flag for inject #5628

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

Closed
johnhargrove opened this issue May 7, 2017 · 3 comments

Comments

@johnhargrove
Copy link

What problem does this feature solve?

Currently when an inject fails to resolve, nothing happens, and the injected value just remains untouched. This seems like an opportunity to warn the developer that an inject could not be resolved.

Perhaps this is intended behavior. Maybe it makes sense that sometimes an injection would not be resolved. If that is the case, could there be a way to indicate a given inject is required? A stricter injection method?

My current workaround is to throw an error if the object is undefined as of create (since injects are resolved by then), but it's a manual step that must be made in many places.

What does the proposed API look like?

If a new API is needed to support this, I am not sure how to design it.

As for warning, I was hoping that when an inject could not be resolved, there would be a warning in the browser console.

@maciej-ka
Copy link
Contributor

@johnhargrove could you give an example of failing inject? I'm not sure when can this happen.

@johnhargrove
Copy link
Author

@Lokson This is any situation in which a child component has an inject which can't be satisfied by a provide in itself or any of its component ancestors, including the root Vue object itself.

I ran into this while developing a validation service. Basically, I have a ValidationService class which is a provide on a top level component which represents a collection of form inputs. The inputs themselves are very complicated, so they are their own Vue components:

 FormComponent (provides ValidationService)
 |-- ComplicatedInput (expects ValidationService inject)
 |-- ComplicatedInput (expects ValidationService inject)

If, when building this, I forget to provide the ValidationService on the parent FormComponent, the ComplicatedInput components will silently never have their reference to ValidationService injected. This results in errors related to accessing members of an undefined object, etc.

My theory here and the reason for this feature request is that getting some kind of indication that an inject went unsatisfied seems useful for development.

@Anamasi30
Copy link

[email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants