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, // 視窗的寬度,對應的是我們設計稿的寬度,移動端一般是750,如果是pc端那就是類似1920這樣的尺寸
// viewportHeight: 1344, // 視窗的高度,移動端一般指定1334,也可以不配置
unitPrecision: 3, // 指定`px`轉換為視窗單位值的小數(shù)位數(shù)(很多時候無法整除)
viewportUnit: "vw", // 指定需要轉換成的視窗單位,建議使用vw
selectorBlackList: ['.nocalssvw'], // 指定不轉換為視窗單位的類,可以自定義,可以無限添加,建議定義一至兩個通用的類名
exclude: [/printPersone/],
// propList:["*", "!font-size"],//能轉化為vw的屬性列表
minPixelValue: 1, // 小于或等于`1px`不轉換為視窗單位,你也可以設置為你想要的值
mediaQuery: false // 允許在媒體查詢中轉換`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>總結
到此這篇關于vue項目純前端實現(xiàn)的模板打印功能的文章就介紹到這了,更多相關vue純前端模板打印功能內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue項目中的支付功能實現(xiàn)(微信支付和支付寶支付)
本文主要介紹了vue項目中的支付功能實現(xiàn)(微信支付和支付寶支付),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01
vue3 emit is not a function問題及解決
這篇文章主要介紹了vue3 emit is not a function問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-09-09

