question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Examples of how to use the modules in a Vue component

See original GitHub issue

Having been frustrated by vuex-typescript I thought I would give this ago but I find that I am struggling without, apparently, any examples of how to import and use the modules (and the getters, actions etc) in a standard Typescript Vue component.

It looks like it could be a useful library but the README is too lightweight at the moment. Any chance of some better, more complete examples?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:22 (7 by maintainers)

github_iconTop GitHub Comments

5reactions
danielroecommented, Jun 10, 2019

@Yackbz In Nuxt, index.ts can have export functions and objects, and that is where your nuxtServerInit() belongs. That function can then call server initialisation functions in modules.

So, for example:

import { ActionTree, Store } from 'vuex'
import Vue from 'vue'

import { initialiseStores, sampleStore } from '~/utils/store-accessor'
import { RootState } from '~/types/state'

const initializer = (store: Store<any>) => initialiseStores(store)
export const plugins = [initializer]

export const actions: ActionTree<RootState, RootState> = {
  async nuxtServerInit(_context, server: { req: IncomingMessage; app: Vue }) {
    await sampleStore.serverInit(server)
  },

  async nuxtClientInit(context) {
    await sampleStore.clientInit()
  },
}

export * from '~/utils/store-accessor'

Note that the above code doesn’t use vuex-module-decorators for the index.ts, but it does for everything else. Also see #124 for an example of what the store-accessor would look like.

3reactions
kpturnercommented, Jan 2, 2019

After much faffing around I defined the module like this:

import { Module, VuexModule, Mutation, Action } from 'vuex-module-decorators';


@Module({ stateFactory: true, name: 'counter' })
export default class CounterModule extends VuexModule {
    private count: number = 0;

    @Mutation
    public increment(delta: number): void {
        this.count += delta;
    }
    @Mutation
    public decrement(delta: number): void {
        this.count -= delta;
    }

    // action 'incr' commits mutation 'increment' when done with return value as payload
    @Action({ commit: 'increment' })
    public incr(): number {
        return 5;
    }
    // action 'decr' commits mutation 'decrement' when done with return value as payload
    @Action({ commit: 'decrement' })
    public decr(): number {
        return 5;
    }

    get getCount(): number {
        return this.count;
    }

}

Then accessed it in my Vue component like this:

import CounterModule from '../store/modules/test_module';
import { getModule } from 'vuex-module-decorators';
let counterModule: CounterModule;

Then

created() {
        counterModule = getModule(CounterModule, this.$store);
}

Then I was able to access methods etc elsewhere - for example

computed: {
        counter() {
            return counterModule.getCount
        }
    }

But it seems a bit contrived. Is this a good way or is there a better way, considering that I am not using vue-class-component ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Separating a Vue project into modules | by Keagan Chisnall
Have you ever wanted to properly separate your Vue project into plugin-type modules? In this example I'll show you the basics of making...
Read more >
Building external modules in VueJS - Pusher Blog
When we create an external module, we can use the same module across multiple projects. ... An example of this is vue-custom-element.
Read more >
Modules | Vuex
To help with that, Vuex allows us to divide our store into modules. Each module can contain its own state, mutations, actions, getters, ......
Read more >
Vuex Modules - A Vue.js Lesson From our Vue.js Course
Each Vuex modules can contain its own state, mutations, actions, getters and even their own modules. In this vue.js tutorial, we'll learn how...
Read more >
Vue Modules, Components & Data Binding | VueJS EP 03
VueJS makes it easy to work with data binding. We'll focus on the basics understanding of data in Javascript and text interpolation.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found