vue項目純前端實現(xiàn)的模板打印功能示例代碼
下載一個插件 npm i vue-print-nb --save
在main中引入 import Print from “vue-print-nb”
Vue.use(Print);
在postcss.config.js里面展示這個數(shù)據(jù)樣式,如果你的項目中沒有這個文件
那就下載一個插件"npm i postcss-px-to-view --save-dev";
module.exports = {
plugins: {
autoprefixer: {},
"postcss-px-to-viewport": {
viewportWidth: 1920, // 視窗的寬度,對應(yīng)的是我們設(shè)計稿的寬度,移動端一般是750,如果是pc端那就是類似1920這樣的尺寸
// viewportHeight: 1344, // 視窗的高度,移動端一般指定1334,也可以不配置
unitPrecision: 3, // 指定`px`轉(zhuǎn)換為視窗單位值的小數(shù)位數(shù)(很多時候無法整除)
viewportUnit: "vw", // 指定需要轉(zhuǎn)換成的視窗單位,建議使用vw
selectorBlackList: ['.nocalssvw'], // 指定不轉(zhuǎn)換為視窗單位的類,可以自定義,可以無限添加,建議定義一至兩個通用的類名
exclude: [/printPersone/],
// propList:["*", "!font-size"],//能轉(zhuǎn)化為vw的屬性列表
minPixelValue: 1, // 小于或等于`1px`不轉(zhuǎn)換為視窗單位,你也可以設(shè)置為你想要的值
mediaQuery: false // 允許在媒體查詢中轉(zhuǎn)換`px`
}
}
};創(chuàng)建一個和“selectorBlackList”里面名字一樣的vue,如上:printPersone.vue
父組件
<template>
<div>
<el-dialog title="表" :visible.sync="dialogVisible" width="70%" :before-close="handleClose">
<el-button type="primary" plain style="margin-bottom:5px;" @click="handlePrint">打印</el-button>
<el-row>
<el-col :span="12">
<div>
<table border="1" class="tableOne" v-for="(item, index) in dataList" :key="index">
<thead>
<tr>
<th>姓名</th>
<td>張三</td>
<th>性別</th>
<td>男</td>
<th>出生年月</th>
<td>1985.5.10</td>
<td rowspan="4" style="width: 130px;"></td>
</tr>
<tr>
<th>民族</th>
<td>漢</td>
<th>籍貫</th>
<td>漢</td>
<th>出生地</th>
<td>山東</td>
</tr>
</thead>
</table>
</div>
</el-col>
</el-row>
<!-- 引用那個打印的模板 -->
<print-person ref="myPrintPerson" :list="dataList" />
</el-dialog>
</div>
</template>
<script>
import PrintPerson from './components/printPersone.vue'
export default {
components: {
PrintPerson,
},
data() {
return {
dialogVisible: false,
dataList: [],
};
},
created() {
},
methods: {
init(dataList) {
this.dialogVisible = true;
this.dataList = dataList;
this.getList();
},
handleClose(done) {
done();
},
// 打印按鈕的事件
handlePrint() {
let refPrint = this.$refs['myPrintPerson']
refPrint && refPrint.print()
},
}
};
</script>打印的模板
打印的模板組件
<template>
<div>
<button ref="printbtn" class="myprintbtn" v-print="'#myprintDom'"></button>
<div id="myprintDom" class="nocalssvw">
<div class="print-warp" style="page-break-before:always;" v-for="(item, ix) in list" :key="ix">
<table border="1" class="primt-table print-tableOne">
<thead>
<tr>
<td style="width: 68px;" class="pt">姓名</td>
<td style="width: 58px;">張三</td>
<td style="width: 68px;" class="pt">性別</td>
<td style="width: 68px;" class="ptw84">男</td>
<td style="width: 68px;" class="pt">出生年月</td>
<td style="width: 68px;">1987.5.10</td>
<td rowspan="4" colspan="2" style="width: 120px;"></td>
</tr>
<tr>
<td class="pt">民族</td>
<td>漢</td>
<td class="pt">籍貫</td>
<td>漢</td>
<td class="pt">出生地</td>
<td>山東</td>
</tr>
</thead>
</table>
</div>
</div>
</div>
</template>
<script>
export default {
props: {
list: {
type: Array,
default: () => [],
required: true,
},
},
data() {
return {
myPrint: {
id: 'myprintDom',
extarCss: ''
}
}
},
methods: {
print() {
this.$refs['printbtn'].click();
}
},
}
</script>總結(jié)
到此這篇關(guān)于vue項目純前端實現(xiàn)的模板打印功能的文章就介紹到這了,更多相關(guān)vue純前端模板打印功能內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vscode搭建vue環(huán)境完整圖文教程(適合新手小白)
Vue框架的優(yōu)秀設(shè)計和強大的生態(tài)系統(tǒng)成為了越來越多開發(fā)者選擇Vue的原因,在實際項目過程中一個高效的開發(fā)環(huán)境能夠大大提高開發(fā)效率,這篇文章主要給大家介紹了關(guān)于vscode搭建vue環(huán)境的相關(guān)資料,需要的朋友可以參考下2023-10-10
關(guān)于vue v-for循環(huán)解決img標簽的src動態(tài)綁定問題
今天小編就為大家分享一篇關(guān)于vue v-for循環(huán)解決img標簽的src動態(tài)綁定問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
Vue組件傳值過程中丟失數(shù)據(jù)的分析與解決方案
這篇文章主要給大家介紹了關(guān)于Vue組件傳值過程中丟失數(shù)據(jù)的分析與解決方案,文中通過圖文介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03
vue 路由視圖 router-view嵌套跳轉(zhuǎn)的實現(xiàn)
這篇文章主要介紹了vue 路由視圖 router-view嵌套跳轉(zhuǎn),主要實現(xiàn)的內(nèi)容有制作一個登錄頁面,跳轉(zhuǎn)到首頁,首頁包含菜單欄、頂部導航欄、主體,標準的后臺網(wǎng)頁格式,菜單點擊顯示不同的頁面,感興趣的小伙伴請參考下面文章內(nèi)容2021-09-09

