Comments (11)
In my case,
It was solved by creating a custom plugin.
- Create Custom Plugin
// 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)
}
- Load Plugin
import Vue from 'vue'
import Vuex from 'vuex'
import { createPersistedState, createSharedMutations } from 'vuex-electron'
import createPromiseAction from './promise-action' // <-- ADD
import modules from './modules'
Vue.use(Vuex)
export default new Vuex.Store({
modules,
plugins: [
createPersistedState(),
createSharedMutations(),
createPromiseAction() // <-- ADD
],
strict: process.env.NODE_ENV !== 'production'
})
- Use
// Action
const actions = {
yourAction ({commit}, items) {
return 'OK'
}
}
// Component
const response = await this.$store.dispatchPromise('yourAction')
console.log(response) // OK
from vuex-electron.
Was looking all over the internet looking why my Promise handling wasn't working.. Would be great to have a fix in the package itself without having to manually create the plugin.
from vuex-electron.
I've been wasting a bunch of time trying to figure out why I can't get this working. I guess this library isn't really in a usable state.
from vuex-electron.
how did you solve this problem???
from vuex-electron.
Thanks for this plugin. I tried it with axios and it works but i get an error message in the renderer console: Unhandled rejection Error: Request failed with status code 400
const actions = {
login ({commit}, data) {
return axios.post(getTokenUrl, data)
.then(r => {
commit('SET_TOKEN', data)
})
},
}
Any idea how to fix this?
from vuex-electron.
@cwirz probably, Your sending request failed (url, data or method is invalid). axios/axios#972
You can use .catch
for catch the error response.
const actions = {
login ({commit}, data) {
return axios.post(getTokenUrl, data)
.then(r => {
commit('SET_TOKEN', data)
})
.catch(e => {
// when request failed
console.log(e)
})
},
}
from vuex-electron.
@ktmouk yeah this kinda worked but i wanted to catch the error in the component from where i dispatch the action:
this.$store.dispatchPromise('auth/login', this.credentials)
.then(r => {
this.skip()
})
.catch(e => {
this.error = e.response.data
})
This time i dont get any error like that anymore so thanks!
from vuex-electron.
In my case I'm usign axios and I return the Promise ( okm.$ws.auth.login really is a method what returns the axios post ):
const actions = {
login({ commit }, userCredentials) {
return okm.$ws.auth.login(userCredentials.username, userCredentials.password).then(response => {
const token = response;
okm.$ws.setToken(token);
commit('setToken', token);
});
}
};
And from the component I'm using in this way ( you can consume the response from the action and the error from the component too ):
methods: {
...mapActions('user', ['login'),
executeLogin() {
this.login({ username: this.username, password: this.password }).then(response => {
// Get user data
....
}).catch(error => {
this.showError(error);
});
}
}
from vuex-electron.
from vuex-electron.
@ktmouk thank you so much for your solution. I was searching why I cant get the async replies using dispatch. If you can please add it as a plugin to npm, so others can easily use this. thanks again :)
from vuex-electron.
The heart of the issue is that there's no way for the result of an action to be passed back to the renderer process.
This was my approach to changing the library, although it needs electron > 7 to use invoke
...
ferm10n@ec3bab4#diff-5ea1fc93a80776ff21fc637ff5967a710b3effa4720112d9b09617662fb2f8afR46-R107
Also I think I might have been dumb and could have just used event.reply()
and not changed the required electron version
just kidding, I thought it would be easy like socket.io does it... I guess there isn't a way to provide a callback in the renderer process? Which means getting the responses from main to resolve the right actions in the renderer would get hella weird.
from vuex-electron.
Related Issues (20)
- Not updating the state HOT 4
- Maintainers required HOT 5
- Unable to use code in actions with createSharedMutations HOT 3
- Locally stored state doesn't get updated and causes weird merges HOT 4
- caching data In nuxt js , vue
- Bump deepmerge
- vuex-electron doesn't support batch deleting by condition from vuex-orm HOT 2
- Could not find a declaration file HOT 1
- dispatch HOT 1
- No way to configure the electron-store instance outside of changing the file name HOT 1
- Failed to serialize arguments HOT 2
- [shared data] Master init failed HOT 3
- [Vuex Electron] Storage is not valid HOT 4
- How to rehydrate after restarting or reloading. HOT 2
- No console or network logged HOT 1
- [conf, ajv] DevTools failed to load SourceMaps HOT 4
- [fix] [Vuex Electron] Storage is not valid. Please, read the docs.
- 'createPersistedState' add options 'cwd'?
- Webpack 5 incompatibility
- the package electron-store is not lastest ,Cannot read properties of undefined (reading 'app') HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vuex-electron.