Vue.js - 将JSON对象写入本地文件

Phe*_*ero 7 javascript json node.js vue.js vuejs2

不久之前,我开始学习Vue.js,不久之后,我开始了一个更大的项目,不记得,Javascript与本地文件系统交互的选项有限.我通过vue-cli设置项目,所以我必须通过网站启动npm start.

所述项目由JSON Files的可视化编辑器组成.当我想实现保存按钮时,我认识到将一个JSON文件写入/保存(并且将来可能会读取)到我的本地计算机是一项非常困难的任务.

我已经尝试过使用node的fs库,认为它可以工作,因为它是用node启动的.我还尝试了几个外部库,例如write-json-filenpm lib.

我已经达到了一个我没有想法的地步,并且几乎可以做任何必要的工作.

yue*_*you 23

有3种方法可以做到这一点.

  1. 写入本地存储

  2. 创建一个Blob并调用一个事件来下载它

  3. 将其包装到电子应用程序中并使用节点fs模块来保存文件

我可以在这里为这3个案例展示一个样本

的index.html

<!DOCTYPE html>
<html lang="en" xmlns:v-on="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Vue test</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.13/dist/vue.js"></script>
</head>
<body>
<div id="vue-app">
    <form>
        <input type="text" v-model="name"/>{{name}}<br/>
        <input type="text" v-model="last"/>{{last}}<br/>
        <input type="text" v-model="index"/>{{index}}<br/>
        <select v-model="grade">
            <option>5</option>
            <option>6</option>
            <option>7</option>
            <option>8</option>
            <option>9</option>
            <option>10</option>
        </select>
        {{grade}}
        <button type="button" v-on:click="add()">Add To Table</button>
        <button type="button" v-on:click="saveFile()">saveFile</button>
    </form>
    <table border="1">
        <thead><td>Name</td><td>Last Name</td><td>Index</td><td>Grade</td></thead>
        <tbody>
        <tr v-for="x in arr">
            <td>{{x.first}}</td>
            <td>{{x.lastn}}</td>
            <td>{{x.index}}</td>
            <td>{{x.grade}}</td>
        </tr>
        </tbody>
    </table>
</div>
<script src="test.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

test.js(写入本地存储)

new Vue ({
  el: '#vue-app',
  data: {
      name: '',
      last: '',
      index: 0,
      grade: 0,
      arr: []
  },

  methods: {
      add: function (e) {
          this.arr.push({first: this.name, lastn: this.last, index: this.index, grade: this.grade});
          console.log(1);
      },
      saveFile: function() {
        const data = JSON.stringify(this.arr)
        window.localStorage.setItem('arr', data);
        console.log(JSON.parse(window.localStorage.getItem('arr')))
  }
});
Run Code Online (Sandbox Code Playgroud)

创建一个Blob并调用一个事件来下载它

只更改saveFile func

saveFile: function() {
    const data = JSON.stringify(this.arr)
    const blob = new Blob([data], {type: 'text/plain'})
    const e = document.createEvent('MouseEvents'),
    a = document.createElement('a');
    a.download = "test.json";
    a.href = window.URL.createObjectURL(blob);
    a.dataset.downloadurl = ['text/json', a.download, a.href].join(':');
    e.initEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
    a.dispatchEvent(e);
}
Run Code Online (Sandbox Code Playgroud)

将其包装到Electron应用程序中并使用节点fs模块保存文件

更改saveFile func

saveFile: function() {
    const data = JSON.stringify(this.arr)
    const fs = require('fs');
    try { fs.writeFileSync('myfile.txt', data, 'utf-8'); }
    catch(e) { alert('Failed to save the file !'); }
}
Run Code Online (Sandbox Code Playgroud)

然后用Electron包裹它

electron ./index.html

  • 制作电子应用程序的唯一目的是让用户下载一些东西,对我来说似乎是严重的矫枉过正。 (3认同)