问题描述
我正在使用 Vue 和 Electron 构建一个桌面应用程序.我想从一个 vue 组件中保存一个文件,其中包含用户介绍的一些数据.为此,我尝试在 vuex 操作中使用 fs 节点模块,但它让我出错.找不到那个模块.我知道 Vue 是客户端,但是,我认为在与 Electron 一起使用时它可以工作,但事实并非如此.为了初始化我的应用程序,我使用了 vue-cli 和命令 vue init webpack electron-vue.我正在使用 vuex 系统和 vuex 模块,我有一个 actions.js 文件,我尝试使用 fs 模块:
I'm building a desktop app using Vue and Electron. I want to save a file from a vue component with some data introduced by the user. For doing that, I tried used fs node module inside an vuex action, but it got me error. Can't found that module. I know Vue is client side, but, I thought that at the moment of using with Electron it could work, but it does't. To init my app I used vue-cli and the command vue init webpack electron-vue. I'm using vuex system and using vuex modules too, I've an actions.js file where I tried to use the fs module:
// import * as fs from 'fs'; I used this way at first
const fs = require('fs');
export const writeToFile = ({commit}) => {
fs.writeFileSync('/path/file.json', JSON.stringify(someObjectHere));
};
当我从 Vue 组件(例如 Options.vue)调用此操作时,我使用 vuex 调度系统,并且在该组件的 created() 方法中:
When I call this action from a Vue component, ex, Options.vue, I use the vuex dispatch system, and, in the created() method of that component:
this.$store.dispatch('writeToFile')
这引起了我上面提到的错误
That's raised me the error above mentioned
推荐答案
要在electron中使用文件系统与Vue和WebPack,文件系统实例必须在执行命令npm run"后在dist/index.html中声明构建"
to use File System in electron with Vue and WebPack, the file system instance has to be declared in the dist/index.html after execute the command "npm run build"
<script>
var fs = require('fs');
</script>
在 vue 组件中,它使用 fs 就像在 vue 文件中声明一样.
and in the vue component, it 's used fs like if it would have been declared in the vue file.
...
export const writeToFile = ({commit}) => {
fs.writeFileSync('/path/file.json', SON.stringify(someObjectHere))
};
...
如果不在 electron 中使用,或者将其写入 dev 的索引中,则会引发错误.
while if not use it in electron or you write it in the index to dev, it throws an error.
这篇关于vue + electron 如何将文件写入磁盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!