vue項(xiàng)目純前端實(shí)現(xiàn)的模板打印功能示例代碼
下載一個(gè)插件 npm i vue-print-nb --save
在main中引入 import Print from “vue-print-nb”
Vue.use(Print);
在postcss.config.js里面展示這個(gè)數(shù)據(jù)樣式,如果你的項(xiàng)目中沒(méi)有這個(gè)文件
那就下載一個(gè)插件"npm i postcss-px-to-view --save-dev";
module.exports = { plugins: { autoprefixer: {}, "postcss-px-to-viewport": { viewportWidth: 1920, // 視窗的寬度,對(duì)應(yīng)的是我們?cè)O(shè)計(jì)稿的寬度,移動(dòng)端一般是750,如果是pc端那就是類似1920這樣的尺寸 // viewportHeight: 1344, // 視窗的高度,移動(dòng)端一般指定1334,也可以不配置 unitPrecision: 3, // 指定`px`轉(zhuǎn)換為視窗單位值的小數(shù)位數(shù)(很多時(shí)候無(wú)法整除) viewportUnit: "vw", // 指定需要轉(zhuǎn)換成的視窗單位,建議使用vw selectorBlackList: ['.nocalssvw'], // 指定不轉(zhuǎn)換為視窗單位的類,可以自定義,可以無(wú)限添加,建議定義一至兩個(gè)通用的類名 exclude: [/printPersone/], // propList:["*", "!font-size"],//能轉(zhuǎn)化為vw的屬性列表 minPixelValue: 1, // 小于或等于`1px`不轉(zhuǎn)換為視窗單位,你也可以設(shè)置為你想要的值 mediaQuery: false // 允許在媒體查詢中轉(zhuǎn)換`px` } } };
創(chuàng)建一個(gè)和“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> <!-- 引用那個(gè)打印的模板 --> <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àng)目純前端實(shí)現(xiàn)的模板打印功能的文章就介紹到這了,更多相關(guān)vue純前端模板打印功能內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vscode搭建vue環(huán)境完整圖文教程(適合新手小白)
Vue框架的優(yōu)秀設(shè)計(jì)和強(qiáng)大的生態(tài)系統(tǒng)成為了越來(lái)越多開(kāi)發(fā)者選擇Vue的原因,在實(shí)際項(xiàng)目過(guò)程中一個(gè)高效的開(kāi)發(fā)環(huán)境能夠大大提高開(kāi)發(fā)效率,這篇文章主要給大家介紹了關(guān)于vscode搭建vue環(huán)境的相關(guān)資料,需要的朋友可以參考下2023-10-10uniapp中設(shè)置全局頁(yè)面背景色的簡(jiǎn)單教程
uni-app如何設(shè)置頁(yè)面全局背景色,其實(shí)非常簡(jiǎn)單,一句話即可,但有時(shí)候也會(huì)踩一些坑,這篇文章主要給大家介紹了關(guān)于uniapp中設(shè)置全局頁(yè)面背景色的相關(guān)資料,需要的朋友可以參考下2023-03-03關(guān)于vue v-for循環(huán)解決img標(biāo)簽的src動(dòng)態(tài)綁定問(wèn)題
今天小編就為大家分享一篇關(guān)于vue v-for循環(huán)解決img標(biāo)簽的src動(dòng)態(tài)綁定問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09Vue組件傳值過(guò)程中丟失數(shù)據(jù)的分析與解決方案
這篇文章主要給大家介紹了關(guān)于Vue組件傳值過(guò)程中丟失數(shù)據(jù)的分析與解決方案,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03使用Elemen加上lang=“ts“后編譯報(bào)錯(cuò)
本文主要介紹了使用Elemen加上lang=“ts“后編譯報(bào)錯(cuò),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04vue 路由視圖 router-view嵌套跳轉(zhuǎn)的實(shí)現(xiàn)
這篇文章主要介紹了vue 路由視圖 router-view嵌套跳轉(zhuǎn),主要實(shí)現(xiàn)的內(nèi)容有制作一個(gè)登錄頁(yè)面,跳轉(zhuǎn)到首頁(yè),首頁(yè)包含菜單欄、頂部導(dǎo)航欄、主體,標(biāo)準(zhǔn)的后臺(tái)網(wǎng)頁(yè)格式,菜單點(diǎn)擊顯示不同的頁(yè)面,感興趣的小伙伴請(qǐng)參考下面文章內(nèi)容2021-09-09