-
-
Notifications
You must be signed in to change notification settings - Fork 17
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
Comments
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! |
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:
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'],
}),
],
});
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:
<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> |
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?
The text was updated successfully, but these errors were encountered: