element icon indicating copy to clipboard operation
element copied to clipboard

upload 组件里动态绑定的 file-list 属性值,没有双向绑定。

Open iamnewsea opened this issue 9 years ago • 13 comments

我在官网的帮助文档里发现 upload 组件可以绑定 file-list , 但是当我添加了文件后, 绑定到 file-list 对象的数据并没有变化。

例子: HTML部分:

<el-upload
	class="avatar-uploader"
	action="http://192.168.1.179:1081/sys/fileUpload"
	:withCredentials="true"
	:show-file-list="true"
	:auto-upload="false"
	ref="logoImage"
	:file-list="logos"
	:multiple="true"
	:on-change="changedabc"
>
<img v-if="logo.url" :src="logo.url" class="avatar">
<i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>

JS:

export default {
    data() {
      return {
        logos: [{name: 'food', url: 'abc.jpg'}],
        logo: {id: "", url: "", netUrl: ""}
      }
    },
    watch: {
      logos: {
        deep: true,
        handler: function () {
          console.log("OK----")
        }
      }
    }
}

当用户选择文件后, 页面显示正常 ,但是数据 logos 没有变化。

iamnewsea avatar Apr 13 '17 09:04 iamnewsea

你好,因为本 issue 不符合我们的规范,所以被关闭了。请使用 issue-generator 提交 issue。相关信息:#3692。

element-bot avatar Apr 13 '17 09:04 element-bot

Vue 2.0 本来就没有双向绑定。

Leopoldthecoder avatar Apr 13 '17 09:04 Leopoldthecoder

Input v-model 就可以双向绑定啊。 vue 都可以双向绑定啊。

iamnewsea avatar Apr 13 '17 10:04 iamnewsea

建议再读读 Vue 文档。

Leopoldthecoder avatar Apr 13 '17 10:04 Leopoldthecoder

看到了。

iamnewsea avatar Apr 13 '17 10:04 iamnewsea

如果你觉得可以双向绑定,那么请解释一下为何这个 demo 里 input 的 disabled 属性在不断变化时,myDisabled 始终保持为 true。

Leopoldthecoder avatar Apr 13 '17 10:04 Leopoldthecoder

你说的对。 i saw

iamnewsea avatar Apr 13 '17 10:04 iamnewsea

当用户选择自动上传为 false 时,用户上传了文件时,能不能添加一个功能,回调我一下下。

我要实现:上传文件后,客户端计算文件的MD5, Ajax一次和服务器进行比较, 如果一致,就不用传了。 如果不一致,就上传。 所以想,自动上传设置为flase , 触发我的事件, 事件里手动上传 。

iamnewsea avatar Apr 13 '17 10:04 iamnewsea

这个功能在 #4192 里同步。

Leopoldthecoder avatar Apr 13 '17 10:04 Leopoldthecoder

ok

iamnewsea avatar Apr 13 '17 10:04 iamnewsea

el-upload怎么在没有选择文件时也能提交?

iamgrf avatar Oct 16 '17 04:10 iamgrf

可以暂时通过其他方法同步list (uploadFiles 属性)

    handleChange(file, fileList) {
      var uploads = this.$refs.upload
      uploads.forEach((item, index) => {
        console.log(item.uploadFiles)
      })
    },

elementui 的 el-upload 的 ref 的值是一个Array :

this.$refs.uploada // uploada: (3) [VueComponent, VueComponent, VueComponent]

通过下标可以取得对应值。 换句话说,得这样取汁:

console.log(this.$refs.uploada[0].uploadFiles)
console.log(this.$refs.uploada[1].uploadFiles)
console.log(this.$refs.uploada[2].uploadFiles)

PLQin avatar Jun 20 '21 02:06 PLQin

提供一个解决思路,把需要绑定的属性值用匿名函数传进去。 :on-success="handleSuccess(fileList)"

handleSuccess($fileList) {
  if (!Array.isArray($fileList)) { $fileList = [] }
  return function (response, file, fileList) {
      file.url = response.result
      $fileList.splice(0, $fileList.length)
      $fileList.push(...fileList)
    }
}

noctiomg avatar Feb 28 '24 03:02 noctiomg