return和return false的区别

 就拿elementui组件中el-upload来说吧,我想上传图片,上传之前要做一些限制。

handleAvatarSuccess(res, file) {
  alert("success")
  this.imageUrl = URL.createObjectURL(file.raw);
},
beforeAvatarUpload(file) {
  const isJPG = file.type === 'image/jpeg';
  const isLt2M = file.size / 1024 / 1024 < 2;

  if (!isJPG) {
    this.$message.error('上传头像图片只能是 JPG 格式!');
    return;
  }
}
这时我添加一个.txt文件,可以弹出"success"。可以证明return是中断方法,handleAvatarSuccess会执行。
如果这时改代码,代码如下:

handleAvatarSuccess(res, file) {
  alert("success")
  this.imageUrl = URL.createObjectURL(file.raw);
},
beforeAvatarUpload(file) {
  const isJPG = file.type === 'image/jpeg';
  const isLt2M = file.size / 1024 / 1024 < 2;

  if (!isJPG) {
    this.$message.error('上传头像图片只能是 JPG 格式!');
    return false;
  }
}
这时我添加一个.txt文件,不能弹出"success"。可以证明return是中断事件,handleAvatarSuccess不会执行。

原文地址:https://www.cnblogs.com/xingzoudecd/p/14078656.html