【问题标题】:this.$refs.selectedImages.files.map is not a function - vue electronthis.$refs.selectedImages.files.map 不是函数 - vue electron
【发布时间】:2021-03-17 06:01:40
【问题描述】:

我正在尝试在我的 vue 组件中使用 map 函数。我有一个文件上传输入,可以接受多个文件。由于我正在使用电子,因此我无法将文件直接传递到应用程序的后端,因此我想使用 Array.prototype.map() 函数获取文件路径并在使用 ipcRenderer.send() 函数传递数组后处理它们。无论如何,我总是收到此错误this.$refs.selectedImages.files.map is not a function 我该如何解决?

这是我正在尝试的实际代码

    handleFiles() {
      this.files = this.$refs.selectedImages.files.map( (file) => file.path ); //this will cause the error
    }, //
    processFiles() {
      this.isLoading = true;
    
      window.ipcRenderer.send('processFiles', this.files);
      
      window.ipcRenderer.on('', (event, data) => {
        console.log(ecent, data);
        saveAs(data, 'processed.mfs')
      });
    } 

更新

为了尝试解决问题,我循环 FilesList 以提取每个文件的路径,然后使用 ipcRenderer 传递它。问题是我无法在background.js 上收到消息,我不知道为什么。

this.$refs.selectedImages.files.forEach( (file) => {
 this.files.push(file.path) // this will push into the array the path of each file
})

window.ipcRenderer.send('processFiles', this.files);

//background.js

import { ipcMain } from 'electron'

ipcMain.on('processFiles', (event, data) => {
 console.log(event, data) // the message will not arrive
})

【问题讨论】:

  • 将您的ipcRenderer.on 语句移至created。如上所述,每次调用processFiles 时都会定义一个新的渲染器侦听器,并在定义任何侦听器之前发送第一条消息。

标签: file vue.js electron


【解决方案1】:

FileList 对象实际上不是一个数组,并且没有.map 方法。但是您可以像这样访问文件:

const files = this.$refs.selectedImages.files;

for (var i = 0; i < files.length; i++) {
   console.log(files[i]);
}

【讨论】:

  • 我需要使用电子的ipcRenderer 传递文件,所以我想避免传递每个文件并将同一事件多次发送到后端,有更好的解决方案吗?
  • 我不使用电子,但看起来你可以像现在一样发送整个 this.files 对象。尝试删除.map
  • 我需要测试但我正在阅读 this question 并且似乎无法使用 ipcRenderer 传递文件对象?如果我尝试我会得到Error: An object could not be cloned.
  • 好的,我明白了。看起来那个问题/答案正在使用我在单个文件对象上看不到的 path 属性,所以我不确定。我不使用电子,我认为您的问题只是尝试在 FileList 对象上使用 .map
  • 在文件对象上使用地图会给我这个问题的标题错误。我尝试在文件列表对象上执行forEach,然后将files.path 推入数组,但似乎ipcRenderer 没有将数据发送到ipcMain
猜你喜欢
  • 2021-07-05
  • 1970-01-01
  • 2019-08-15
  • 2021-03-31
  • 2018-01-19
  • 2012-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多