Vue encapsulates the public function method of exporting Excel data

Vue encapsulates the public function method of exporting Excel data

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:

insert image description here

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

insert image description here

insert image description here

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:

insert image description here

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

insert image description here

In general, interface interception only needs to return data, such as: const res = response.data, return res

insert image description here

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:
  • How to export the excel file returned by the backend in Vue front end
  • Vue requests the backend interface to export excel tables
  • How to use js-xlsx to export excel in vue
  • Detailed implementation plan of Vue front-end exporting Excel files
  • Detailed explanation of Excel parsing and exporting based on Vue
  • Pitfalls and solutions when exporting excel using vue

<<:  Manjaro installation CUDA implementation tutorial analysis

>>:  How to choose the format when using binlog in MySQL

Recommend

Detailed explanation of the general steps for SQL statement optimization

Preface This article mainly shares with you the g...

React implements a general skeleton screen component example

Table of contents What is a skeleton screen? Demo...

Install Linux rhel7.3 operating system on virtual machine (specific steps)

Install virtualization software Before installing...

A brief discussion on JavaScript shallow copy and deep copy

Table of contents 1. Direct assignment 2. Shallow...

Node and Python two-way communication implementation code

Table of contents Process Communication Bidirecti...

Summary of the minesweeping project implemented in JS

This article shares the summary of the JS mineswe...

Detailed explanation of the usage of Object.assign() in ES6

Table of contents 2. Purpose 2.1 Adding propertie...

Summarize the User-Agent of popular browsers

1. Basic knowledge: Http Header User-Agent User A...

Use Docker to create a distributed lnmp image

Table of contents 1. Docker distributed lnmp imag...

Solution to nginx hiding version number and WEB server information

Nginx can not only hide version information, but ...

Mobile front-end adaptation solution (summary)

I searched online and found that many interviews ...

VMware virtualization kvm installation and deployment tutorial summary

Virtualization 1. Environment Centos7.3 Disable s...

Configure VIM as a C++ development editor in Ubuntu

1. Copy the configuration file to the user enviro...

XHTML Getting Started Tutorial: Form Tags

<br />Forms are an important channel for use...