npm install papaparse

 data: function() {
    return {
      isShowLast: false,
      tableData: [],
      itemList: [
        {
          name: "红木",
          type: "P000001",
          price: "¥88888"
        },
        {
          name: "水杉",
          type: "P000002",
          price: "¥2000"
        }
      ]
    };
  },
  methods: {
    download() {
      console.log("downLoad");
      var csv = Papa.unparse(this.itemList);
      console.log("downLoad", csv);
      //定义文件内容,类型必须为Blob 否则createObjectURL会报错
      let content = new Blob([csv]);
      //生成url对象
      let urlObject = window.URL || window.webkitURL || window;
      let url = urlObject.createObjectURL(content);
      //生成<a></a>DOM元素
      let el = document.createElement("a");
      //链接赋值
      el.href = url;
      el.download = "todo文件导出.cvs";
      //必须点击否则不会下载
      el.click();
      //移除链接释放资源
      urlObject.revokeObjectURL(url);
    }
}

 

Logo

前往低代码交流专区

更多推荐