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

Document Vue 3 / vuex 4 compatibility #36

Closed
johtso opened this issue Apr 17, 2021 · 2 comments
Closed

Document Vue 3 / vuex 4 compatibility #36

johtso opened this issue Apr 17, 2021 · 2 comments
Assignees
Labels
question Further information is requested

Comments

@johtso
Copy link

johtso commented Apr 17, 2021

In a landscape where there is mixed support for the latest version of Vue, it would be great to have a small mention of version compatibility.

From glancing through closed issues, it seems like the project works fine with vuex 4, right?

@gabrielmbmb
Copy link
Owner

gabrielmbmb commented Apr 18, 2021

Hi @johtso, I have not tested the plugin with Vue 3 and Vuex 4 on my own, but I think it should work fine. Anyway, I'll try to do some testing and document it this week!

@gabrielmbmb gabrielmbmb added the question Further information is requested label Apr 18, 2021
@gabrielmbmb gabrielmbmb self-assigned this Apr 18, 2021
@gabrielmbmb
Copy link
Owner

Hi @johtso ! I've checking the compatibility of the plugin with Vue 3 and Vuex 4 these days and I didn't found any problem.

The code I written is in TypeScript, and I used it to sync the locale between several tabs:

index.ts

import { createStore } from 'vuex';
import locale, { LocaleState } from './locale.module';
import createMultiTabState from 'vuex-multi-tab-state';

export interface RootState {
  locale: LocaleState;
}

export default createStore({
  modules: {
    locale,
  },
  plugins: [
    createMultiTabState({
      statesPaths: ['locale'],
    }),
  ],
});

locale.module.ts

import { ActionContext, ActionTree, GetterTree, MutationTree } from 'vuex';
import { RootState } from './index';
import I18n from '@/plugins/i18n';

export enum ActionTypes {
  CHANGE_LOCALE = 'changeLocale',
}

export enum MutationTypes {
  SET_LOCALE = 'setLocale',
}

// State
export interface LocaleState {
  locale: string;
}

const state: LocaleState = {
  locale: process.env.VUE_APP_I18N_LOCALE,
};

// Getters
export interface Getters {
  getLocale(state: LocaleState): string;
}

const getters: GetterTree<LocaleState, RootState> & Getters = {
  getLocale(state) {
    return state.locale;
  },
};

// Actions
export interface Actions {
  [ActionTypes.CHANGE_LOCALE](
    context: ActionContext<LocaleState, RootState>,
    locale: string
  ): void;
}

const actions: ActionTree<LocaleState, RootState> & Actions = {
  [ActionTypes.CHANGE_LOCALE](context, locale) {
    I18n.global.locale.value = locale;
    context.commit(MutationTypes.SET_LOCALE, I18n.global.locale);
  },
};

// Mutations
export interface Mutations {
  [MutationTypes.SET_LOCALE](state: LocaleState, locale: string): void;
}

const mutations: MutationTree<LocaleState> & Mutations = {
  [MutationTypes.SET_LOCALE](state, locale) {
    state.locale = locale;
  },
};

export default { state, getters, actions, mutations };

Then I used the store inside a component using the new Vue 3 Composition API:

LocaleSelector.vue

<script lang="ts">
import { useStore } from 'vuex';
import { defineComponent, computed, WritableComputedRef } from 'vue';
import { ActionTypes } from '@/store/locale.module.ts';
import es from '@/assets/icons/es.svg';
import gb from '@/assets/icons/gb.svg';

// Available languages
const langs = [
  {
    value: 'es',
    name: 'Español',
    icon: es,
  },
  { value: 'en', name: 'English', icon: gb },
];

export default defineComponent({
  name: 'LocaleSelector',
  setup() {
    const store = useStore();
    const locale: WritableComputedRef<string> = computed({
      get(): string {
        return store.getters.getLocale;
      },
      set(newLocale: string): void {
        store.dispatch(ActionTypes.CHANGE_LOCALE, newLocale);
      },
    });

    return {
      locale,
      langs,
    };
  },
});
</script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants