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

Allow defining optional inject dependency with default values #6097

Closed
pdanpdan opened this issue Jul 13, 2017 · 3 comments · Fixed by #6322
Closed

Allow defining optional inject dependency with default values #6097

pdanpdan opened this issue Jul 13, 2017 · 3 comments · Fixed by #6322

Comments

@pdanpdan
Copy link
Contributor

What problem does this feature solve?

In case of components that can work both as standalone and as children, it would be nice to be able to provide default values for the not provided dependencies.
It would also avoid the warning for missing inject in case this is one of the expected use case.

What does the proposed API look like?

inject: Array | { [key: string]: string | Symbol | { name: string | Symbol, default: any } }

@kasperisager
Copy link

Ditto on this request. What I typically find myself doing is overriding the injected property as data in order to provide a default:

inject: [
  'foo'
],
data () {
  return {
    foo: this.foo || 'default'
  }
}

@bundyo
Copy link

bundyo commented Jul 14, 2017

I'm also +1 for this feature. Currently I have components that are nested within each other and parents provide info to the children. The warning always pops up for the root component as it doesn't have a provider for the injected property.

Specific use case: nested layout splitters.

@privatenumber
Copy link
Contributor

I'm working on a component library that utilizes provide and inject for certain components to behave/look slightly differently when in the context of another component of the library. Since our components are used by other engineers for development, warnings that the injection was not found keeps showing up in their environment.

Would love this feature to be implemented. And to add to it, as mentioned in #6137, I'd like to see a required attribute to indicate whether an injection is required or optional.

chrisvfritz pushed a commit to vuejs/v2.vuejs.org that referenced this issue Mar 10, 2018
* close #6097: Allow defining optional inject dependency with default values

Document changes in inject definition for feature #6097

* reformatted description of inject options

* further clarification of inject options description
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants