Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

InjectReactive Default doesnt work. #238

Open
AliLozano opened this issue Aug 9, 2019 · 3 comments
Open

InjectReactive Default doesnt work. #238

AliLozano opened this issue Aug 9, 2019 · 3 comments
Labels

Comments

@AliLozano
Copy link

Code

  @InjectReactive({ from: 'messages', default: '' }) 
  @ProvideReactive('messages')
  public message!: string;

Behavior expected:

  • When message is not provided, use default: ''

Results:

  • Injection "reactiveInject" not found
AliLozano added a commit to AliLozano/vue-property-decorator that referenced this issue Aug 9, 2019
@Conaire
Copy link

Conaire commented Nov 22, 2019

Any update on this?

@Conaire
Copy link

Conaire commented Nov 22, 2019

Using @AliLozano 's commit, if you don't want to wait for a merge you can just create your own decorator:

function InjectReactiveFixed(options?: InjectOptions | InjectKey) {
    return createDecorator((componentOptions, key) => {
        if (typeof componentOptions.inject === 'undefined') {
            componentOptions.inject = {}
        }
        if (!Array.isArray(componentOptions.inject)) {
            const fromKey = !!options ? (options as any).from || options : key
            const defaultVal = (!!options && (options as any).default) || undefined
            if (!componentOptions.computed) componentOptions.computed = {}
            componentOptions.computed![key] = function() {
                const obj = (this as any)[reactiveInjectKey]
                return obj ? obj[fromKey] : defaultVal
            }

            //@ts-ignore
            componentOptions.inject[reactiveInjectKey] = {key: reactiveInjectKey, default: defaultVal}


        }
    })
}

@kaorun343 kaorun343 added the bug label Nov 20, 2020
@Eterion
Copy link

Eterion commented Dec 3, 2020

The solution provided by @Conaire doesn't work, at least not for me.
So I'm using this instead.

export function InjectReactive(options?: InjectOptions | InjectKey) {
  return createDecorator((componentOptions, key) => {
    if (typeof componentOptions.inject === 'undefined') {
      componentOptions.inject = {};
    }
    if (!Array.isArray(componentOptions.inject)) {
      const fromKey = options ? (options as any).from || options : key;
      const defaultVal =
        (Boolean(options) && (options as any).default) || undefined;
      if (!componentOptions.computed) componentOptions.computed = {};
      componentOptions.computed![key] = function () {
        const obj = (this as any)[reactiveInjectKey];
        // return obj ? obj[fromKey] : defaultVal
        // >>> This is where to set the default value
        return typeof obj[fromKey] === 'undefined' ? defaultVal : obj[fromKey];
      };
      componentOptions.inject[reactiveInjectKey] = reactiveInjectKey;
    }
  });
}

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

No branches or pull requests

4 participants