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
<br />When uploading on some websites, a [Se...
Table of contents 1. Repeated declaration 1.1 var...
1. Problem symptoms Version: MySQL 5.6, using the...
What is a profile? We can use it when we want to ...
Find the problem When retrieving the top SQL stat...
This article introduces how to create an index on...
Table of contents Problem Description Historical ...
Table of contents 1. JavaScript is single-threade...
1: masterha_check_repl replica set error replicat...
Preface I have been busy developing a cold chain ...
This article shares with you a js special effect ...
Preface In case of application bug or DBA misoper...
1. The vertical-align property achieves the follo...
Why use Server-Side Rendering (SSR) Better SEO, s...
1. Subquery MySQL 4.1 and above support subquerie...