最近有个模板下载的需求,因为考虑到是静态文件,直接前端做不用后端再写接口,就把文件放在前端,前端实现下载了
<template>
<div>
<button @click="downloadTemplate">下载模板</button>
</div>
</template>
<script>
export default{
methods:{
downloadTemplate(){
const anchor = document.createElement("a");
anchor.href = `../../static/模板.xlsx`;
anchor.setAttribute("download", '模板.xlsx');
anchor.style.display = "none";
document.body.appendChild(anchor);
setTimeout(() => {
anchor.click();
document.body.removeChild(anchor);
setTimeout(() => { self.URL.revokeObjectURL(anchor.href); }, 250);
}, 66);
}
}
}
</script>