简体   繁体   English

Vue.js Element-UI和axios文件上传导致页面刷新

[英]Vue.js Element-UI and axios file upload causing page refresh

I am using the element-ui file upload component( https://element.eleme.io/#/en-US/component/upload ) to handle the frontend of image uploading. 我正在使用element-ui文件上传组件( https://element.eleme.io/#/en-US/component/upload )处理图像上传的前端。 everything is working perfectly(file is sent to the server etc). 一切正常(文件发送到服务器等)。 However for some strange reason after the the axios's successful response code executes, the page refreshes automatically. 但是,在执行axios的成功响应代码后,由于某些奇怪的原因,页面会自动刷新。 I have tried sending the same post request without the file and the page doesnt automatically refresh. 我尝试发送不带文件的相同帖子请求,并且页面不会自动刷新。

The submit button type is set to "button" so that is not the issue in this case. 提交按钮类型设置为“按钮”,因此在这种情况下这不是问题。

Code for axios post request: axios发布请求的代码:

base.post('/add/visit/', formData,   {headers: {'Authorization' : 'Bearer ' + localStorage.getItem('token') ,'Content-Type': 'multipart/form-data'}})

    .then(response => {
      console.log(response);
      resolve(response)
    })
    .catch(err => {
      console.log(err)
      reject(err)
    })
})

I wonder if it has a default behavior like a form submit happening. 我想知道它是否具有默认行为,例如发生表单提交。 If there were no href property it would reload the page. 如果没有href属性,它将重新加载页面。 Is there an event you can tap into like onClick(e)? 有没有可以像onClick(e)一样参与的活动? Then in the function that handles it do e.preventDefault(); 然后在处理它的函数中执行e.preventDefault();。

https://www.w3schools.com/jsref/event_preventdefault.asp https://www.w3schools.com/jsref/event_preventdefault.asp

Element-ui upload can upload the file for you, then there is no page refresh: Element-ui上载可以为您上载文件,然后没有页面刷新:

el-upload(:action="formUploadLocation" :data="form" :auto-upload="false" ref="upload" :file-list="list"
          :on-success="handleSucces" :on-error="handleFail"

Then the upload in the form submit (only when :auto-upload=false): 然后以表单提交形式上传(仅在:auto-upload = false时):

this.$refs.upload.submit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM