vue + electronic如何将文件写入磁盘 [英] vue + electron how to write a file to disk
问题描述
我正在使用Vue和Electron构建桌面应用程序。我想使用用户引入的一些数据来保存Vue组件中的文件。为此,我尝试在vuex动作中使用fs节点模块,但出现错误。找不到该模块。我知道Vue是客户端,但是,我认为在与Electron一起使用时,它可以工作,但不能。要初始化我的应用程序,我使用了vue-cli和命令vue init webpack electronic-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
推荐答案
要将文件系统与Vue和WebPack一起在电子版中使用,必须在执行命令 npm run build后在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))
};
...
如果不将其用于电子或将其写入索引开发人员,则会引发错误。
while if not use it in electron or you write it in the index to dev, it throws an error.
这篇关于vue + electronic如何将文件写入磁盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!