Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
huihongme opened this issue Feb 12, 2019 · 3 comments

Comments

@huihongme
Copy link

huihongme commented Feb 12, 2019

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"

@NoelDavies
Copy link

NoelDavies commented Mar 8, 2019

@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

@larbijirari
Copy link

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

@akodkod
Copy link
Contributor

akodkod commented Aug 29, 2019

#44

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants