Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nuxt.js custom store folder

I tried to change vuex store path that is used by nuxt.js by default. I want my store path to be 'modules/my-module/store/store.js'. Is there any way I can do this? Or maybe I can somehow add my module store to existing store from nuxt module file?

like image 521
Alexander Orlovskiy Avatar asked Jun 28 '26 00:06

Alexander Orlovskiy


1 Answers

Yes, you can register a store module dynamically using the registerModule function.

Let's say you have your my-module module and inside you have your index.vue file. Inside that file you can register you module's store like this:

import store from './store'; //import your module store
export default {
  name: 'my-module',
  computed: {
    ...
  },
  created() {
    this.$store.registerModule('myModuleStore', store);
  },
  mounted() {
    this.$store.dispatch('myModuleStore/someAction'); //example of action for your module's store
  },
};

I recommend you this medium article that suggests some good vue.js application structures, including store modules where it shows how to register a private module

like image 162
Allkin Avatar answered Jun 29 '26 18:06

Allkin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!