vuex-electron icon indicating copy to clipboard operation
vuex-electron copied to clipboard

my vue electron project store use vuex-electron cause “vuex actions” promise not working properly

Open huihongme opened this issue 6 years ago • 3 comments

store actions: delNotes ({dispatch, commit}, id) { return new Promise((resolve, reject) => { setTimeout(() => { resolve() }, 2000) }) }

component: [Vue warn]: Error in event handler for "on-ok": "TypeError: Cannot read property 'then' of undefined"

huihongme avatar Feb 12 '19 04:02 huihongme

@huihongme, I have it working fine on my local machine, can you show the code you're using? Also, have you included a plugin to allow promises to work in vuex actions?

My working setup: src/renderer/store/index.js

import Vue from 'vue'
import Vuex from 'vuex'

import { createSharedMutations } from 'vuex-electron'
import createPromiseAction from './promise-action'
import modules from './modules'

Vue.use(Vuex)

export default new Vuex.Store({
  modules,
  plugins: [
    createSharedMutations(),
    createPromiseAction()
  ],
  strict: process.env.NODE_ENV !== 'production'
})

./src/renderer/store/promise-action.js

import promiseIpc from 'electron-promise-ipc' // yarn add electron-promise-ipc

const DISPATCH = 'promise-action-dispatch'

export default (options = {}) => store => {
  function renderer () {
    store.dispatchPromise = (type, payload) =>
      promiseIpc.send(DISPATCH, {
        type,
        payload
      })
  }

  function main (store) {
    promiseIpc.on(DISPATCH, ({ type, payload }) => {
      return store.dispatch(type, payload)
    })
  }

  return process.type === 'renderer'
    ? renderer()
    : main(store)
}

Hope that helps

NoelDavies avatar Mar 08 '19 12:03 NoelDavies

i shall just add, in the component //myComponet.vue . . . methods:{ myAjaxCall(){ this.$store.dispatchPromise('MyAction').then(.....) // instead of this.$store.dispatch('MyAction').then(.....) }

larbijirari avatar Mar 29 '19 10:03 larbijirari

https://github.com/vue-electron/vuex-electron/issues/44

akodkod avatar Aug 29 '19 14:08 akodkod