请教大佬们在使用 electron 对 Vue 项目打包成 exe 的问题?

316 天前
 godleon

环境

vue2
electron 25.2.0
node 14
electron-store 8.1.0

问题

我使用 electron 对一个 Vue 项目打包成 exe 程序时,按照官方文档已经完全配置好,然后打包成功,但是我在运行 exe 程序的时候,会出现 token 一直丢失的问题。代码里拦截请求,然后登录拿到 token 加到请求头里

1.使用 cooike ,不能登录
2.使用 localstorage ,可以登录到首页,调其他接口又会丢失

查询资料里,建议使用 electron-store,我参考官方文档配置好后,代码不能正常启动

我的代码:

const Store = require('electron-store');

const eStore = new Store();

/**
 * 请求拦截
 */
http.interceptors.request.use(config => {
  config.headers['token'] = eStore.get('token')
  // config.headers['token'] = Vue.cookie.get('token')
  return config
}, error => {
  return Promise.reject(error)
})

报错信息

 ERROR  Failed to compile with 1 errors                                                                                                                                                            下午 6:32:55

error  in ./node_modules/electron-store/index.js

Module parse failed: D:\money\counter\v1.7\counter-fast\counter-vue\node_modules\electron-store\index.js Unexpected token (53:3)
You may need an appropriate loader to handle this file type.
|               options = {
|                       name: 'config',
|                       ...options
|               };
|

@ ./node_modules/babel-loader/lib!./node_modules/vue-loader/lib/selector.js?type=script&index=0&bustCache!./src/views/common/login.vue 87:26-51
@ ./src/views/common/login.vue
@ ./src/views ^\.\/.*\.vue$
@ ./src/router/import-development.js
@ ./src/router/index.js
@ ./src/main.js
@ multi (webpack)-dev-server/client?http://localhost:8001 webpack/hot/dev-server babel-polyfill ./src/main.js

ps

一名后端仔,请教一下这是什么原因造成的,看源码他应该会找 appData 下的 userData.json ,文件来存储,但是没找到这个文件,我建了个文件也是这个错 不太理解...

1732 次点击
所在节点    程序员
14 条回复
henyi2211
316 天前
换 electron-builder 试试,没用过 electron-store 没啥建议可提供
musi
316 天前
只看你 get 没看你 set ,数据从哪来?
godleon
316 天前
@musi 现在的情况是 我只写 const Store = require('electron-store'); const eStore = new Store(); 就启动不起来
musi
316 天前
@godleon 贴一下你的 babel 版本,看起来是 babel 不支持展开语法,但是为什么你的 Node 代码都需要 bable 编译?
godleon
316 天前
"babel-core": "6.22.1",
"babel-eslint": "7.1.1",
"babel-jest": "21.0.2",
"babel-loader": "7.1.1",
"babel-plugin-dynamic-import-node": "1.2.0",
"babel-plugin-transform-es2015-modules-commonjs": "6.26.0",
"babel-plugin-transform-runtime": "6.22.0",
"babel-preset-env": "1.3.2",
"babel-preset-stage-2": "6.22.0",
"babel-register": "6.22.0",
@musi
L3ve
316 天前
问题应该是,electron-store 是后端模块,但是你 babel 去编译了它,这个应该是你整个构建流程有问题,本质上即使后端需要编译,也不应该去编译 node_modules 里的东西
duan602728596
316 天前
babel 都已经是 v7 版本了,为什么还在用 v6 呢?
musi
316 天前
@godleon 根据你的报错链路看了一下,electron-store 应该是在 node 进程用的包,但是你在渲染进程用了,所以会有一系列的问题,正确做法是把 electron-store 放到 node 进程然后用 ipc 和渲染进程通信拿到数据,另外 electron-store 应该只用于数据持久化,你可以在前端也加个缓存这样可以减轻 ipc 的压力
zsj1029
316 天前
session 直接存 cookie localstore 什么的足矣,没必要用到底层存储,简单业务复杂化
godleon
316 天前
@zsj1029 cookie localstore ,我都用了,打包成 exe 的时候会丢失,但是在浏览器正常
va3rrw
316 天前
最近业余在写 eletron APP ,以下是我的做法:
va3rrw
316 天前
最近业余在写 electron APP ,以下是我的做法:

在`main/index.ts`中,

```
import log from 'electron-log'

Store.initRenderer()
```

同时在 BrowserWindow 的定义中:
```
webPreferences: {
preload: join(__dirname, '../preload/index.js'),
contextIsolation: false,
sandbox: false,
},
```

在 `preload/index.ts`中,

```
import storeAPI from 'electron-store'

// @ts-ignore (define in dts)
window.store = storeAPI

```

在`preload/index.d.ts`中,

```
import storeAPI from 'electron-store'

declare global {
interface Window {
store: storeAPI
}
}

```

在 renderer 中用到 store 的地方:
```
// @ts-ignore override warning
const persistentStore = new window.store({ name: 'settings' })

// 使用 persistentStore.get() 读数据
// 使用 persistentStore.set() 写数据
```

我自己对 typescript/electron 也是一知半解, 以上供参考。
va3rrw
316 天前
个人认为 @musi 指出的方向是正确的,electron-store 是 main 进程的,在 renderer 进程里只能通过 preload 或者 ipc 访问。

顺便问一下,手误发出的帖子没法删除或修改?似乎也不支持 markdown 语法?
shalingye
315 天前
看到最后一句 PS 稍微明白了,你可能没打包外部资源?
"extraResources": [
{
"from": "./resources/appData/userData.json",
"to": "./appData/userData.json"
},
]

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/952946

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX