javascript - 在 vue.js 中更改文件输入

标签 javascript vue.js vuejs2 vue-component

使用纯 HTML/JS,可以像这样查看输入元素的选定文件的 JavaScript 文件对象:

<input type="file" id="input" multiple onchange="handleFiles(this.files)">

但是,当将它转换为“Vue”方式时,它似乎并没有按预期工作,而是简单地返回 undefined 而不是返回一个 File 对象数组。

这是它在我的 Vue 模板中的样子:

<input type="file" id="file" class="custom-file-input" 
  v-on:change="previewFiles(this.files)" multiple>

previewFiles 函数简单如下(位于方法中):

  methods: {
    previewFiles: function(files) {
      console.log(files)
    }
  }

是否有替代/正确的方法来执行此操作?谢谢

最佳答案

另一种解决方案:

<input type="file" @change="previewFiles" multiple>

methods: {
   previewFiles(event) {
      console.log(event.target.files);
   }
}

关于javascript - 在 vue.js 中更改文件输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45179061/

相关文章:

typescript - 你如何使用 typescript 接口(interface)输入提示 Vue Prop ?

javascript - 更改 HH :MM:SS 的时间格式

vue.js - NuxtJS/VueJS : How to know if page was rendered on client-side only?

javascript - 在 Angular 5 中禁用 zone.js 后不会触发 ngOnInit

javascript - Gulp - 错误 SyntaxError : Unexpected token

javascript - 运行时 npm WARN 已弃用 'vue init webpack my-project'

webpack - 为什么我无法使用 Webpack 编译 SASS?

javascript - Vue 与 bootstrap-vue : prevent more than one expanded list-element at all times in list (v-for)

javascript - 如何在循环内将元素传递给 jQuery post

javascript - 哪个 child 拥有最多糖果 算法问题