Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 450|回复: 0

Vue+Electron下Vuex的Dispatch没有效果的解决方案

[复制链接]
  • TA的每日心情
    奋斗
    2024-4-6 11:05
  • 签到天数: 748 天

    [LV.9]以坛为家II

    2034

    主题

    2092

    帖子

    70万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    705612
    发表于 2021-5-11 11:54:46 | 显示全部楼层 |阅读模式

    这个问题是解决基于 vue 和 electron 的开发中使用 vuex 的 dispatch 无效的问题,即解决了 Please, don't use direct commit's, use dispatch instead of this. 问题。
    先允许我梳理一下目录结构,以便阅读的时候不会一头雾水,你到底说的这个文件是哪个……
    1558185192156
    其中 /src/main 是存放主配置文件的,/src/render 下面有 storeroutercomponents 等。
    components 下面就是很多 .vue 文件,router 下面就是一些路由配置的 js 文件和一些拦截器的 js
    关键是 storestore 下面有一个 index.js 的主配置文件 index.js,和一个 modules 文件夹。
    index.js 里面写的是(记住这句话,后面会用到):

    import Vue from 'vue'
    import Vuex from 'vuex'
    import { createPersistedState, createSharedMutations } from 'vuex-electron'
    import modules from './modules'
    Vue.use(Vuex)
    export default new Vuex.Store({
      modules,
      plugins: [
        createPersistedState(),
        createSharedMutations()
      ],
      strict: process.env.NODE_ENV !== 'production'
    })
    

    modules/ 下面存放各个实体,例如上图中的 Auth.jsCounter.js,并通过 index.js 全部引入。

    /** * The file enables `@/store/index.js` to import all vuex modules * in a one-shot manner. There should not be any reason to edit this file. */
    const files = require.context('.', false, /\.js$/)
    const modules = {}
    files.keys().forEach(key => {
      if (key === './index.js') return
      modules[key.replace(/(\.\/|\.js)/g, '')] = files(key).default
    })
    export default modules
    

    然后来看一个 vuex 的官方样例:

    const state = {
      main: 0
    }
    const mutations = {
      DECREMENT_MAIN_COUNTER (state) {
        state.main--
      },
      INCREMENT_MAIN_COUNTER (state) {
        state.main++
      }
    }
    const actions = {
      someAsyncTask ({ commit }) {
        // do something async
        commit('INCREMENT_MAIN_COUNTER')
      }
    }
    export default {
      state,
      mutations,
      actions
    }
    

    之后很显然的,我想要在 Vue 的组件调用 INCREMENT_MAIN_COUNTER 对计数器加 1。

    this.$store.commit('INCREMENT_MAIN_COUNTER');
    // this.$store.commit('INCREMENT_MAIN_COUNTER', payload);
    

    如果是一般的 vue,就 OK 了,但是,我遇到了报错,说,Please, don't use direct commit's, use dispatch instead of this.
    1558184959796
    那好吧,没事,不就是不然用 Commit,非要用 Dispatch 嘛,那我就写一个 Action,里面直接调用 Mutation,就像这个样子:

    const actions = {
      JUST_INCREASE ({ commit }) {
        commit('INCREMENT_MAIN_COUNTER')
      }
    }
    

    然而奇怪的事情是,this.$store.dispatch('JUST_INCREASE') 并不能运行,没反应,计数器还是 0,不能赋值,就像是这个函数没有被执行一样。没有报错,没有任何异常,查也查不出什么问题。
    1558184998501
    1558184982642
    网上的资料似乎也挺少。
    折腾了很久,后来发现是 vuex-electron 里面一个插件的锅。
    解决方法有两个。
    方法一:
    store/index.js 里面,就是上文特别强调了的那个文件,去掉 createSharedMutations 插件。

    import Vue from 'vue'
    import Vuex from 'vuex'
    import { createPersistedState, createSharedMutations } from 'vuex-electron'
    import modules from './modules'
    Vue.use(Vuex)
    export default new Vuex.Store({
      modules,
      plugins: [
        createPersistedState(),
        createSharedMutations() // 注释掉这一行
      ],
      strict: process.env.NODE_ENV !== 'production'
    })
    

    这是因为 vuex-electron 引入了一个用于多进程间共享 Vuex Store 的状态的插件。如果没有多进程交互的需求,完全可以不引入这个插件。
    注释掉以后重启项目,用 this.$store.commit('XXX') 就可以使用了。
    然而,如果需要多进程来处理怎么办?
    方法二:
    https://github.com/vue-electron/vuex-electron#installation
    看第 3 条:

    In case if you enabled createSharedMutations() plugin you need to create an instance of store in the main process. To do it just add this line into your main process (for example src/main.js):

      import './path/to/your/store'
    

    这种时候就不能用第一种方法来解决问题了。
    好在文档也说了,加上一行导入。
    找到 /src/main/index.js,在前面加上一句:

    import '../renderer/store'
    

    1558185071126
    之后一切正常,可以使用 Dispatch 来进行操作了。
    1558185678300
    最后还有一个比较奇怪的问题:
    1558185917570
    在直接调用 state 的时候,这样写 this.$store.state.loginStatus 是不行的,会 undefined,必须写成 this.$store.state.Auth.loginStatus,就像是 this.$store.state.Counter.main 一样,似乎可以解释为,不同的模块不指定名字的话就找不到。
    但是,在写 Dispatch 的时候又不需要指定名字了,直接 dispatch('changeLoginStatus') 就行了,不然难道不应该是也按照 dispatch('Auth/changeLoginStatus') 这样子来写嘛……

    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-19 09:12 , Processed in 0.066111 second(s), 29 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表