1、设计好demo.vue页面(略)
2、编写print.js文件,改文件内容如下:
// 打印类属性、方法定义/* eslint-disable */constPrint=function (dom, options) { if (!(thisinstanceofPrint)) returnnewPrint(dom, options); this.options=this.extend({ 'noPrint': '.no-print' }, options); if ((typeofdom) ==="string") { this.dom=document.querySelector(dom); } else { this.isDOM(dom) this.dom=this.isDOM(dom) ?dom : dom.$el; } this.init(); }; Print.prototype= { init: function () { varcontent=this.getStyle() +this.getHtml(); this.writeIframe(content); }, extend: function (obj, obj2) { for (varkinobj2) { obj[k] =obj2[k]; } returnobj; }, getStyle: function () { varstr="", styles=document.querySelectorAll('style,link'); for (vari=0; i<styles.length; i++) { str+=styles[i].outerHTML; } str+="<style>"+ (this.options.notPrint?this.options.notPrint : '.no-print') +"{display:none;}</style>"; str+="<style>.results{width:100%!important;} .result .title{width:100%;}</style>"; returnstr; }, getHtml: function () { varinputs=document.querySelectorAll('input'); vartextareas=document.querySelectorAll('textarea'); varselects=document.querySelectorAll('select'); varcanvass=document.querySelectorAll('canvas'); for (vark=0; k<inputs.length; k++) { if (inputs[k].type=="checkbox"||inputs[k].type=="radio") { if (inputs[k].checked==true) { inputs[k].setAttribute('checked', "checked") } else { inputs[k].removeAttribute('checked') } } elseif (inputs[k].type=="text") { inputs[k].setAttribute('value', inputs[k].value) } else { inputs[k].setAttribute('value', inputs[k].value) } } for (vark2=0; k2<textareas.length; k2++) { if (textareas[k2].type=='textarea') { textareas[k2].innerHTML=textareas[k2].value } } for (vark3=0; k3<selects.length; k3++) { if (selects[k3].type=='select-one') { varchild=selects[k3].children; for (variinchild) { if (child[i].tagName=='OPTION') { if (child[i].selected==true) { child[i].setAttribute('selected', "selected") } else { child[i].removeAttribute('selected') } } } } } //canvass echars图表转为图片for (vark4=0; k4<canvass.length; k4++) { varimageURL=canvass[k4].toDataURL("image/png"); varimg=document.createElement("img"); img.src=imageURL; img.setAttribute('style', 'max-width: 100%;'); img.className='isNeedRemove'canvass[k4].style.display='none'canvass[k4].parentNode.style.width='100%'canvass[k4].parentNode.style.textAlign='center'canvass[k4].parentNode.insertBefore(img,canvass[k4].nextElementSibling); } //做分页//style="page-break-after: always"varpages=document.querySelectorAll('.result'); for (vark5=0; k5<pages.length; k5++) { pages[k5].setAttribute('style', 'page-break-after: always'); } returnthis.dom.outerHTML; }, writeIframe: function (content) { varw, doc, iframe=document.createElement('iframe'), f=document.body.appendChild(iframe); iframe.id="myIframe"; iframe.style="position:absolute;width:0;height:0;top:-10px;left:-10px;"; // iframe.setAttribute('style', 'position:absolute;width:' + document.querySelector('.results').clientWidth + 'px;height:0;top:-10px;left:-10px;');w=f.contentWindow||f.contentDocument; doc=f.contentDocument||f.contentWindow.document; doc.open(); doc.write(content); doc.close(); varremoves=document.querySelectorAll('.isNeedRemove'); for (vark=0; k<removes.length; k++) { removes[k].parentNode.removeChild(removes[k]); } var_this=thisiframe.onload=function(){ _this.toPrint(w); setTimeout(function () { document.body.removeChild(iframe) }, 100) } }, toPrint: function (frameWindow) { try { setTimeout(function () { frameWindow.focus(); try { if (!frameWindow.document.execCommand('print', false, null)) { frameWindow.print(); } } catch (e) { frameWindow.print(); } frameWindow.close(); }, 10); } catch (err) { console.log('err', err); } }, isDOM: (typeofHTMLElement==='object') ?function (obj) { returnobjinstanceofHTMLElement; } : function (obj) { returnobj&&typeofobj==='object'&&obj.nodeType===1&&typeofobj.nodeName==='string'; } }; constMyPlugin= {} MyPlugin.install=function (Vue, options) { // 4. 添加实例方法Vue.prototype.$print=Print} exportdefaultMyPlugin
3、在vue的main.js文件中添加上面的print.js文件,设置全局引用该文件
importPrintfrom'../static/js/print';
Vue.use(Print) // 注册
4、在demo.vue中的添加打印按钮,如下
<el-buttonclass="no-print"type="primary"icon="document"style="margin:20px 48%" @click="print">打印</el-button>,并在print方法添加如下代码,实现打印功能
print(){ letself=this; self.$print(self.$refs.print,{'notPrint':'.no-print'}); },