vue.js 导出JSON

cnpm install file-saver --save

 

 

<template>
  <div class="hello">
    <button @click="exportJSON">导出jsonn</button>
    <button @click="exportText">导出Text</button>
  </div>
</template>

<script>
import FileSaver from 'file-saver'
export default {
  data () {
    return {
      // 待导出的json数据
      CfgInfo: {
        CAN: {
          Chn: 0,
          name: 'CAN通道'
        },
        LIN: {
          Chn: 1,
          name: 'LIN通道'
        }
      }
    }
  },
  methods: {
    exportJSON () {
      // 将json转换成字符串
      const data = JSON.stringify(this.CfgInfo)
      const blob = new Blob([data], {type: ''})
      // const textStr = 'aaaaa,bbbbbbb,cccccc'
      FileSaver.saveAs(blob, 'hahaha.txt')
    },
    exportText () {
      const textStr = 'aaaaa,bbbbbbb,cccccc'
      FileSaver.saveAs(textStr, 'a.txt')
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>

 

posted @ 2019-07-25 18:39  漫步CODE人生  阅读(3771)  评论(0编辑  收藏  举报