vue+element UI encapsulates a public function to export Excel data Encapsulate the public methods in the common.js of the store's modules, as shown below: The code is as follows: const download = { actions: { downloadData({ commit, state }, data) { return new Promise((resolve, reject) => { data.event(data.formData).then(res => { const blob = new Blob([res.data], { type: 'application/vnd.ms-excel' }) const objectUrl = URL.createObjectURL(blob) const link = document.createElement('a') // Create a tag link.href = objectUrl // Rename the file link.download = res.headers['content-disposition'].split( '=' )[1] link.click() URL.revokeObjectURL(objectUrl) resolve(res) }).catch((err) => { reject(err) }) }) } } } export default download Then export it to the public module in the store's index Use in components that need to call methods methods: { //Method for exporting data handleExport(formData) { this.loading = true const data = { // import { loanDownloadData } from '@/api/loan/userLoanList' // event: loanDownloadData, loanDownloadData is the keyword of the interface for exporting data //formData is the parameter required by the loanDownloadData interface event: '', formData: formData } this.$store.dispatch('downloadData', data).then(res => { this.loading = false }).catch(() => { this.loading = false }) }, } PS: If the interface can return data normally, but an error occurs when calling the interface, it may be that the data returned by the interface interception file is incorrect. First find the utils->request.js file (usually placed in this location), as follows: When the interface is intercepted, if the export interface needs to return all the data, such as response, because the headers rename file needs to be used in the common.js file, as follows In general, interface interception only needs to return data, such as: const res = response.data, return res This is the end of this article about Vue encapsulation of public functions for exporting Excel data. For more relevant Vue export Excel public function content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Manjaro installation CUDA implementation tutorial analysis
>>: How to choose the format when using binlog in MySQL
Preface This article mainly shares with you the g...
Table of contents What is a skeleton screen? Demo...
Install virtualization software Before installing...
Table of contents 1. Direct assignment 2. Shallow...
Table of contents Process Communication Bidirecti...
This article shares the summary of the JS mineswe...
Table of contents 2. Purpose 2.1 Adding propertie...
1. Basic knowledge: Http Header User-Agent User A...
Table of contents 1. Docker distributed lnmp imag...
Nginx can not only hide version information, but ...
I searched online and found that many interviews ...
Data backup and restoration part 2, as follows Ba...
Virtualization 1. Environment Centos7.3 Disable s...
1. Copy the configuration file to the user enviro...
<br />Forms are an important channel for use...