vue.js Export 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:
    }
      }'The LIN channel'

    

        }
  },
  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>

 

Guess you like

Origin www.cnblogs.com/scode2/p/11246192.html