Skip to content

Example on how to use with Nuxt #89

Open
@SandroMaglione

Description

@SandroMaglione

I am trying to make the library work with Nuxt.

The docs say that what you need is:

export class UserStore extends createModule({ target: "nuxt" }) {
  ...
}

To test it, I created a new file in the store folder of my Nuxt project. The file contains the class of the example in the docs:

import { createModule, mutation, action } from 'vuex-class-component'

export class UserStore extends createModule({
  strict: false,
  target: 'nuxt'
}) {
  firstname = 'Michael'
  lastname = 'Olofinjana'
  specialty = 'JavaScript'

  @mutation clearName() {
    this.firstname = ''
    this.lastname = ''
  }

  @action async doSomethingAsync() {
    const val1 = await new Promise(() => this.firstname)
    return val1
  }

  @action async doAnotherAsyncStuff(payload: { func: Function }) {
    const number = await this.doSomethingAsync()
    payload.func()
    return payload + this.fullname + number
  }

  // Explicitly define a vuex getter using class getters.
  get fullname() {
    return this.lastname + this.firstname
  }

  // Define a mutation for the vuex getter.
  // NOTE this only works for getters.
  set fullname(name: string) {
    const names = name.split(' ')
    this.firstname = names[0]
    this.lastname = names[1]
  }

  get bio() {
    return `Name: Specialty: ${this.specialty}`
  }
}

I would expect this code to create a new module with the name of the file and relative state (firstname, lastname, specialty), and the getters defined by the class, as well as actions and mutations.

The code compiles, but then the store seems to be empty (from Vuejs devtool):
user: Object (empty), and getter are not present at all.

How can I make it work? Am I missing something?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions