Skip to content

Instantly share code, notes, and snippets.

@natemate90
Last active November 17, 2018 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natemate90/e1c51d4bc2068ac683941a5e1f6ab5a4 to your computer and use it in GitHub Desktop.
Save natemate90/e1c51d4bc2068ac683941a5e1f6ab5a4 to your computer and use it in GitHub Desktop.
Nuxt Vuex Store with dynamic translation slugs example code
methods: {
deLocale() {
if (this.hasPageSlugs) {
return (
this.localePath(this.slicedPath, "de") +
"/" +
this.currentPageSlugs.de
);
}
return this.switchLocalePath("de");
},
enLocale() {
if (this.hasPageSlugs) {
return (
this.localePath(this.slicedPath, "en") +
"/" +
this.currentPageSlugs.en
);
}
return this.switchLocalePath("en");
}
},
computed: {
...mapState({
currentPageSlugs: state => state.currentPageSlugs
}),
...mapGetters(["hasPageSlugs"]),
localeActive() {
return this.$i18n.locale;
},
slicedPath() {
return this.$route.matched[0].name.substring(
0,
this.$route.matched[0].name.lastIndexOf("-")
);
}
}
import Vuex from "vuex";
const createStore = () => {
return new Vuex.Store({
state: () => ({
currentPageSlugs: null
}),
getters: {
hasPageSlugs: state => {
return state.currentPageSlugs !== null;
}
},
mutations: {
setCurrentPageSlugs(state, payload) {
state.currentPageSlugs = payload;
},
resetCurrentPageSlugs(state) {
state.currentPageSlugs = null;
}
}
});
};
export default createStore;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment