Vue實(shí)現(xiàn)瀏覽器打印功能的代碼
Vue實(shí)現(xiàn)瀏覽器打印功能
實(shí)際項(xiàng)目中使用vue實(shí)現(xiàn)調(diào)用本地打印機(jī)打印功能
import vueEasyPrint from "vue-easy-print";
1.導(dǎo)入 “vue-easy-print”
2.編寫打印模板
<template>
<div>
<div >
<!-- 分頁 -->
<div class='tab_company_out'>
<table cellpadding='0' cellspacing='0'>
<tr>
<th width='5%'>用戶昵稱</th>
<th width='25%'>歸屬部門</th>
<th width='5%'>手機(jī)號(hào)碼</th>
<th width='10%'>郵箱</th>
<th width='5%'>用戶名稱</th>
<th width='8%'>用戶性別</th>
<th width='8%'>狀態(tài)</th>
<th width='12%'>崗位</th>
<th width='12%'>角色</th>
<th width='10%'>備注</th>
</tr>
<!-- 每頁顯示onePageRow條數(shù)據(jù) -->
<tr >
<td>{{tableData.nickName}}</td>
<td>{{tableData.deptId}}</td>
<td>{{tableData.phonenumber}}</td>
<td>{{tableData.email}}</td>
<td>{{tableData.userName}}</td>
<td>{{tableData.sex}}</td>
<td>{{tableData.status}}</td>
<td>{{tableData.userName}}</td>
<td>{{tableData.userName}}</td>
<td></td>
</tr>
</table>
</div>
</div>
</div>
</template>
<script>
export default {
name: "printUser",
// 制作打印模版組件時(shí),props區(qū)域盡量保留。
props: {
// 接受的打印數(shù)據(jù)
tableData: {},
// 每頁多少行
onePageRow: {
type: Number,
default: 5
},
// 是否插入空白行
blankLines: {
type: Boolean,
default: true
},
getChineseNumber: Function // 求數(shù)字的中文寫法,從easyPrint組件傳入
},
computed: {
pages() {
console.log(this.tableData);
// 求當(dāng)前數(shù)據(jù)能打印的頁數(shù)
/* var pages_ = Math.ceil(this.tableData.detail.length / this.onePageRow); // 向上取整數(shù)*/
// return pages_ <= 0 ? 1 : pages_;
return 1;
},
chineseTotal() {
// 計(jì)算中文合計(jì),如果忘記傳入
return this.getChineseNumber != null
? this.getChineseNumber(this.tableData.total_amount)
: "您還沒有傳入getChineseNumber";
}
},
methods: {
test() {
console.log("21111111111111");
console.log("test");
}
}
};
</script>
<style scoped>
* {
padding: 0;
margin: 0;
list-style-type: none;
font-family: "微軟雅黑";
font-size: 12px;
}
.tab_company_out {
text-align: center;
width: 100%;
margin: auto;
page-break-after: always;
}
h3 {
font-size: 14px;
}
.dan {
text-align: center;
position: relative;
}
.dan span {
position: absolute;
right: 0;
}
p {
overflow: hidden;
padding: 10px 0;
}
p span {
float: left;
}
p span ins {
text-decoration: underline;
}
p time {
float: right;
}
table {
width: 100%;
border: none;
border-bottom: 1px solid #000;
}
table tr td {
border: 1px solid #000;
border-bottom: none;
border-right: none;
height: 20px;
line-height: 20px;
}
table tr td:last-of-type,
table tr th:last-of-type {
border-right: 1px solid #000;
}
table tr th {
border-top: 1px solid #000;
border-left: 1px solid #000;
height: 22px;
line-height: 22px;
font-size: 12px;
}
table tr th:nth-child(2) {
width: 0;
}
.lu {
display: inline-block;
padding-top: 10px;
}
.lu li {
float: left;
text-align: left;
margin-right: 15px;
}
.lu li label {
width: 100px;
display: inline-block;
}
.lu li:last-of-type {
margin-right: 0;
}
@page{
size: auto A4 landscape;
margin: 3mm;
}
</style>
3.在需要添加打印功能的界面引入打印模板
import printUser from "./printUser";
4.注冊(cè)模板 printUser 和vueEasyPrint
components: { vueEasyPrint,printUser },
5.添加打印按鈕。
el-button size="mini" type="text" icon="el-icon-edit"
@click="printDemo(scope.row)" v-hasPermi="['system:user:edit']" >打印
**<vue-easy-print** tableShow ref="easyPrint" v-show="false" >
<template slot-scope="func">
**<print-user** :getChineseNumber="func.getChineseNumber" :tableData="tabledata">**</print-user>**
</template>
**</vue-easy-print>**
</el-button>
6.將要打印的內(nèi)容傳值到模板
printDemo(row) {
this.reset();
const userId = row.userId || this.ids;
getUser(userId).then(response => {
this.tabledata = response.data;
//注:此處使用延時(shí)的原因是,防止點(diǎn)擊打印都,打印內(nèi)容還未渲染到模板,導(dǎo)致打印頁面顯示空白。
setTimeout(() =>{
this.$refs.easyPrint.print();
},100);
});
},
7.打印模板接收值并賦值到打印模板(打印模板可根據(jù)業(yè)務(wù)需求自行調(diào)整)
export default {
name: "printUser",
// 制作打印模版組件時(shí),props區(qū)域盡量保留。
props: {
// 接受的打印數(shù)據(jù),此處父子組件傳值,在子組件(模板)定義一個(gè)對(duì)象(若為集合或者其他類型,自行定義)tableData,用于接收父組件傳遞的值,
tableData: {},
// 每頁多少行
onePageRow: {
type: Number,
default: 5
},
// 是否插入空白行
blankLines: {
type: Boolean,
default: true
},
getChineseNumber: Function // 求數(shù)字的中文寫法,從easyPrint組件傳入
},
computed: {
pages() {
console.log(this.tableData);
// 求當(dāng)前數(shù)據(jù)能打印的頁數(shù)
/* var pages_ = Math.ceil(this.tableData.detail.length / this.onePageRow); // 向上取整數(shù)*/
// return pages_ <= 0 ? 1 : pages_;
return 1;
},
chineseTotal() {
// 計(jì)算中文合計(jì),如果忘記傳入
return this.getChineseNumber != null
? this.getChineseNumber(this.tableData.total_amount)
: "您還沒有傳入getChineseNumber";
}
},
methods: {
test() {
console.log("21111111111111");
console.log("test");
}
}
};
實(shí)現(xiàn)功能的界面如下:


總結(jié)
到此這篇關(guān)于Vue實(shí)現(xiàn)瀏覽器打印功能的文章就介紹到這了,更多相關(guān)vue 瀏覽器打印內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue開發(fā)利器之unplugin-auto-import的使用
unplugin-auto-import 解決了vue3-hook、vue-router、useVue等多個(gè)插件的自動(dòng)導(dǎo)入,也支持自定義插件的自動(dòng)導(dǎo)入,下面這篇文章主要給大家介紹了關(guān)于vue開發(fā)利器之unplugin-auto-import使用的相關(guān)資料,需要的朋友可以參考下2023-02-02
vue使用$attrs和$listeners多級(jí)組件嵌套傳遞數(shù)據(jù)
這篇文章主要為大家介紹了vue使用$attrs和$listeners多級(jí)組件嵌套傳遞數(shù)據(jù)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
VUE-ElementUI?時(shí)間區(qū)間選擇器的使用
這篇文章主要介紹了VUE-ElementUI?時(shí)間區(qū)間選擇器的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
vue-admin-template配置快捷導(dǎo)航的代碼(標(biāo)簽導(dǎo)航欄)
這篇文章主要介紹了vue-admin-template配置快捷導(dǎo)航的方法(標(biāo)簽導(dǎo)航欄),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
vue實(shí)現(xiàn)類似淘寶商品評(píng)價(jià)頁面星級(jí)評(píng)價(jià)及上傳多張圖片功能
最近在寫一個(gè)關(guān)于vue的商城項(xiàng)目,然后集成在移動(dòng)端中,開發(fā)需求中有一界面,類似淘寶商城評(píng)價(jià)界面!接下來通過本文給大家分享vue實(shí)現(xiàn)類似淘寶商品評(píng)價(jià)頁面星級(jí)評(píng)價(jià)及上傳多張圖片功能,需要的朋友參考下吧2018-10-10

