vuejs / vuex

🗃️ Centralized State Management for Vue.js.
https://vuex.vuejs.org
MIT License
28.41k stars 9.58k forks source link

Add useStore for v3 for vue 2.7 with composition api #2212

Open ThomasKientz opened 1 year ago

ThomasKientz commented 1 year ago

What problem does this feature solve?

Vue 2.7 supports composition API but we can't use it with Vuex (v3).

What does the proposed API look like?

Porting useStore from v4 to v3. Or make v4 compatible with vue 2.7.

yanyalin commented 1 year ago

https://blog.csdn.net/qq_16139383/article/details/119935755 @ThomasKientz S econdary packaging required

dgautsch commented 1 year ago

@ThomasKientz Did you find a workaround?

ThomasKientz commented 1 year ago

@dgautsch no

citrusjunoss commented 1 year ago

I need the hook too.

zlx362211854 commented 1 year ago

You can create your own useStore,

// utils file
import {getCurrentInstance} from 'vue'
export const useStore = () => {
  const vm = getCurrentInstance();
  if (!vm) throw new Error('must be called in setup');
  return vm.proxy.$store;
};

You can use it anywhere you want:

import { useStore } from '@/utils/index';
const store = useStore();

@ThomasKientz