Write json file vuejs
Ngày đăng:
21/11/2021
Trả lời:
0
Lượt xem:
189
Vue.js - Write JSON object to local fileThere are 3 ways to do this.
I can show you a sample here for these 3 cases index.html
test.js (Write to local storage) 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'))) } });Create a Blob and invoke a event to download it only change for saveFile func Wrap it into an Electron app and use node fs module to save file Change for 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 !'); } }Then use Electron to wrap it electron ./index.html |