vue中excel文件 打包后不展示问题
问题:vue获取模板excel文件时,不从接口获取,从本地文件获取,打包后出现文件不存在情况
解决办法:vue打包之后,excel放置在静态文件里
public里新建文件夹excel存放.xlsx文件
在需要展示文件的地方,通过axios解析获取文件
<template><div><el-button @click="getDemoExcel">获取模板文件</el-button></div>
</template>
<script setup lang="ts">
import axios from 'axios';
import { ElMessage } from 'element-plus';const getDemoExcel = () => {axios.get('/excel/demo.xlsx', {responseType: 'blob',}).then((response: any) => {const url = window.URL.createObjectURL(new Blob([response.data]));const link = document.createElement('a');link.href = url;link.setAttribute('download', '模板.xlsx');document.body.appendChild(link);link.click();document.body.removeChild(link);}).catch((error: any) => {ElMessage.error('文件不存在')})
}
</script>
打包之后,文件与assest同级