composition-api icon indicating copy to clipboard operation
composition-api copied to clipboard

vue2 use provide / inject ,get undefined

Open jeremyjone opened this issue 2 years ago • 3 comments

I created a component by vue2(v2.6.14). It use provide and inject in @vue/composition-api to create a local state management.

export const initStore = () => {
    const data = reactive([]);
    provide('data1', data);
    ...
}
export const useStore = () => {
    return {
        data1: inject('data1');
        ....
    }
}

In root component initStore, and useStore everywhere in component. In test, it looks OK.

When I publish package, and use it, all inject returns undefined. the custom component can not run normally.

It can not use like that or what do I need to do?

jeremyjone avatar Aug 23 '22 09:08 jeremyjone

I met same problem

JerryWu1234 avatar Oct 06 '22 01:10 JerryWu1234

In development the currentInstance got object , when in production that's value got null

JerryWu1234 avatar Oct 06 '22 01:10 JerryWu1234

@jeremyjone Do you know how to reproduction?

JerryWu1234 avatar Oct 10 '22 07:10 JerryWu1234

Stale issue message

github-actions[bot] avatar Dec 09 '22 23:12 github-actions[bot]

I solved the problem. Just fix the version number to v2.6.14. Because npm is automatically installed in the latest version, which is v2.7.x. They are in conflict.

jeremyjone avatar Dec 10 '22 02:12 jeremyjone