郑州专门做网站的公司有哪些,网站建设第三方,深圳市做网站公司,建筑公司简介范文大全需求导入的下载模版不想放在服务器放在前端本地下载静态资源最简单的方式直接访问 public 文件夹下的文件
方法一#xff1a;使用静态文件路径 将文件放在 public 文件夹中#xff1a; 把你的文件从 src/assets 移动到 public 文件夹。例如#xff1a;public/template.xls。…需求导入的下载模版不想放在服务器放在前端本地下载静态资源最简单的方式直接访问 public 文件夹下的文件
方法一使用静态文件路径 将文件放在 public 文件夹中 把你的文件从 src/assets 移动到 public 文件夹。例如public/template.xls。 在 Vue 组件中使用相对路径
templatedivbutton clickdownloadTemplate下载模板/button/div
/templatescript
export default {methods: {downloadTemplate() {// 使用相对路径const fileUrl /template.xls;// 创建一个隐藏的a标签const link document.createElement(a);link.href fileUrl;link.download template.xls; // 设置下载文件名// 将a标签添加到DOM并触发点击事件document.body.appendChild(link);link.click();// 移除a标签document.body.removeChild(link);}}
}
/scriptstyle scoped
/* 你的样式代码 */
/style
二、确保项目目录如下
my-vue-project/
├── public/
│ └── template.xls
├── src/
│ ├── assets/
│ ├── components/
│ └── App.vue
└── vue.config.js
三、验证 启动你的开发服务器通常是 npm run serve或npm run dev。 直接在浏览器中访问 http://localhost:9000/template.xlsx 看是否能访问到文件。
通过以上步骤应该能够确保静态文件能够正确访问和下载