vue后台数据接入

在项目中如果要接的后台数据接口量大,则需要将接口统一管理,将axios封装后 在页面中相应的位置上调用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Vue界面中设计一个表单,让用户输入相关数据,可以使用Vue的表单组件。具体步骤如下: 1. 在Vue组件的template模板中,使用Vue的表单组件,例如`<input>`、`<textarea>`、`<select>`等,让用户输入表单数据。 ```html <template> <form> <label for="title">标题</label> <input type="text" id="title" v-model="title" /> <label for="content">内容</label> <textarea id="content" v-model="content"></textarea> <label for="type">类型</label> <select id="type" v-model="type"> <option value="1">类型1</option> <option value="2">类型2</option> <option value="3">类型3</option> </select> <button type="button" @click="exportWord()">导出Word</button> </form> </template> ``` 2. 在Vue组件的`data`选项中,定义表单数据的初始值。 ```js export default { data() { return { title: '', content: '', type: '1' }; } }; ``` 3. 在Vue组件中,定义一个导出Word文档的方法`exportWord()`。该方法通过调用后台接口,传递表单数据,调用Word模板,将数据填入模板中,最后将填好数据的Word文档输出到前端。 ```js export default { methods: { exportWord() { // 通过axios调用后台接口,传递表单数据 axios.post('/api/exportWord', { title: this.title, content: this.content, type: this.type }, { responseType: 'blob' // 指定响应类型为二进制流 }).then(res => { // 将响应的二进制流文件转成Blob对象 const blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' }); // 创建一个a标签,设置下载链接和文件名,模拟点击下载 const a = document.createElement('a'); const fileName = `导出的Word文档-${new Date().toLocaleDateString()}.docx`; a.href = URL.createObjectURL(blob); a.download = fileName; a.click(); }).catch(error => { console.log(error); }); } } }; ``` 4. 在后台接口中,接收前端传递的表单数据,根据数据填充Word模板,最后将生成的Word文档以二进制流的形式返回给前端。 这里涉及到Word模板的填充,可以使用第三方库docxtemplater来实现。具体步骤可以参考docxtemplater的官方文档。 总体的代码示例可以参考下面的代码: ```html <template> <form> <label for="title">标题</label> <input type="text" id="title" v-model="title" /> <label for="content">内容</label> <textarea id="content" v-model="content"></textarea> <label for="type">类型</label> <select id="type" v-model="type"> <option value="1">类型1</option> <option value="2">类型2</option> <option value="3">类型3</option> </select> <button type="button" @click="exportWord()">导出Word</button> </form> </template> <script> import axios from 'axios'; export default { data() { return { title: '', content: '', type: '1' }; }, methods: { exportWord() { axios.post('/api/exportWord', { title: this.title, content: this.content, type: this.type }, { responseType: 'blob' }).then(res => { const blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' }); const a = document.createElement('a'); const fileName = `导出的Word文档-${new Date().toLocaleDateString()}.docx`; a.href = URL.createObjectURL(blob); a.download = fileName; a.click(); }).catch(error => { console.log(error); }); } } }; </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值