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

Prevent self inject #1818

Closed
CyberAP opened this issue Aug 9, 2020 · 2 comments
Closed

Prevent self inject #1818

CyberAP opened this issue Aug 9, 2020 · 2 comments

Comments

@CyberAP
Copy link
Contributor

CyberAP commented Aug 9, 2020

What problem does this feature solve?

There's a problem with current provide\inject behaviour when component provides the same key it injects. If you try to do that you'll inject the same value that is provided by the component. The problem with that is that it's impossible to override provisions in a single component, which makes nested provisions unnecessary complicated. In order to do that you'll have to split provide\inject into two components and somehow share or even duplicate data between these two components (usually by manually cycling through this.$parent chain).

A common example of deep provision are breadcrumbs:

export default {
  provide() {
    const breadcrumbs = [];
    if (this.breadcrumbs) breadcrumbs = [...this.breadcrumbs];
    breadcrumbs.push(this.$router.currentRoute)
    return { breadcrumbs }
  },
  inject: ['breadcrumbs']
}

This provision could be used on nested page components to automatically generate components for each route level.
The code above obviously doesn't work because injected and provided breadcrumbs would be equal.

It's also unclear for me what's the use-case for self-injection. If there's not such case I think self-injection can be safely prevented and at the same time open opportunities for cleaner provide\inject techniques.

What does the proposed API look like?

The suggestion is that self-injection should be prevented by default.

@posva
Copy link
Member

posva commented Aug 10, 2020

Can you provide a boiled down repro?

@CyberAP
Copy link
Contributor Author

CyberAP commented Aug 10, 2020

Turns out injection hook is called before provide, so it is not possible to self-inject. Sorry for the inconvenience.

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

No branches or pull requests

2 participants