2

I try to create a new Object of an Class at a click on a Button and want to store this Object in the vuex state. For Example, when a button is clicked, it calls the method "newMine()":

newMine() {
        this.$store.dispatch('setNewMine',"Iron")
      }

in the Store, i have this action:

import { Mine } from './modules/mine'
...
state: {
    mines:[]
  },
  mutations: {
    setNewMine(state,ore){
      state.mines.push(new Mine(ore))
    }
  },
  actions: {
    setNewMine({commit},ore){
      commit('setNewMine',ore)
    }
  },
  getters: {
    getMines:(state) =>{
      return state.mines
    }
  }

the class "Mine" looks like this:

export default class Mine {
    constructor(id,wert){
        this.id = id
        this.wert = wert
    }
    get id(){
        return this.id
    }
      set id(id) {
        this.id = id
      }
      get wert(){
        return this.wert
      }
      set wert(wert) {
        this.id = wert
      }
  }

It should be really simple, but it still doesnt work, because i get the error message:

[Vue warn]: Error in v-on handler: "TypeError: _modules_mine__WEBPACK_IMPORTED_MODULE_3__.Mine is not a constructor"

Whats wrong here? Any help is welcomed, even if it without the store. Thank you

1 Answer 1

2

import { Mine } from './modules/mine' results in Mine === undefined, so it causes is not a constructor error when instantiated with new.

export default class Mine {...} is default export. So it needs to be imported as:

import Mine from './modules/mine'
Sign up to request clarification or add additional context in comments.

2 Comments

tried without the brackets, but then i get this error message: "Error in v-on handler: "InternalError: too much recursion"
It's because of id member. You're getting/setting a property that calls a getter/setter of itself, this results in recursion. In case you want to provide getter/setter pair for id (this is an antipattern in this case because it doesn't differ from regular this.id access), there should be a private property, set id(id) { this._id = id } and get id(){ return this._id }.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.