vue 项目 打印, 要求 只打印div里面的内容
修改过的打印方法

1 // 打印类属性、方法定义 2 /* eslint-disable */ 3 const Print = function (dom, options) { 4 if (!(this instanceof Print)) return new Print(dom, options); 5 6 this.options = this.extend({ 7 'noPrint': '.no-print' 8 }, options); 9 10 if ((typeof dom) === "string") { 11 this.dom = document.querySelector(dom); 12 } else { 13 this.isDOM(dom) 14 this.dom = this.isDOM(dom) ? dom : dom.$el; 15 } 16 17 this.init(); 18 }; 19 Print.prototype = { 20 init: function () { 21 var content = this.getStyle() + this.getHtml(); 22 this.writeIframe(content); 23 }, 24 extend: function (obj, obj2) { 25 for (var k in obj2) { 26 obj[k] = obj2[k]; 27 } 28 return obj; 29 }, 30 31 getStyle: function () { 32 var str = "", 33 styles = document.querySelectorAll('style,link'); 34 for (var i = 0; i < styles.length; i++) { 35 if (styles[i].nodeName === 'STYLE'){ 36 str += styles[i].outerHTML; 37 } else if (styles[i].nodeName === 'LINK' && styles[i].href.indexOf('css') > -1){ 38 str += styles[i].outerHTML; 39 } 40 } 41 str += "<style>" + (this.options.noPrint ? this.options.noPrint : '.no-print') + "{display:none;}</style>"; 42 43 return str; 44 }, 45 46 getHtml: function () { 47 var inputs = document.querySelectorAll('input'); 48 var textareas = document.querySelectorAll('textarea'); 49 var selects = document.querySelectorAll('select'); 50 51 for (var k = 0; k < inputs.length; k++) { 52 if (inputs[k].type == "checkbox" || inputs[k].type == "radio") { 53 if (inputs[k].checked == true) { 54 inputs[k].setAttribute('checked', "checked") 55 } else { 56 inputs[k].removeAttribute('checked') 57 } 58 } else if (inputs[k].type == "text") { 59 inputs[k].setAttribute('value', inputs[k].value) 60 } else { 61 inputs[k].setAttribute('value', inputs[k].value) 62 } 63 } 64 65 for (var k2 = 0; k2 < textareas.length; k2++) { 66 if (textareas[k2].type == 'textarea') { 67 textareas[k2].innerHTML = textareas[k2].value 68 } 69 } 70 71 for (var k3 = 0; k3 < selects.length; k3++) { 72 if (selects[k3].type == 'select-one') { 73 var child = selects[k3].children; 74 for (var i in child) { 75 if (child[i].tagName == 'OPTION') { 76 if (child[i].selected == true) { 77 child[i].setAttribute('selected', "selected") 78 } else { 79 child[i].removeAttribute('selected') 80 } 81 } 82 } 83 } 84 } 85 // 包裹要打印的元素 86 // fix: https://github.com/xyl66/vuePlugs_printjs/issues/36 87 // let outerHTML = this.wrapperRefDom(this.dom).outerHTML // 向父级查找 88 let outerHTML = this.dom.innerHTML 89 return outerHTML; 90 }, 91 // 向父级元素循环,包裹当前需要打印的元素 92 // 防止根级别开头的 css 选择器不生效 93 wrapperRefDom: function (refDom) { 94 let prevDom = null 95 let currDom = refDom 96 // 判断当前元素是否在 body 中,不在文档中则直接返回该节点 97 if (!this.isInBody(currDom)) return currDom 98 99 while (currDom) { 100 if (prevDom) { 101 let element = currDom.cloneNode(false) 102 element.appendChild(prevDom) 103 prevDom = element 104 } else { 105 prevDom = currDom.cloneNode(true) 106 } 107 108 currDom = currDom.parentElement 109 } 110 111 return prevDom 112 }, 113 114 writeIframe: function (content) { 115 var w, doc, iframe = document.createElement('iframe'), 116 f = document.body.appendChild(iframe); 117 iframe.id = "myIframe"; 118 //iframe.style = "position:absolute;width:0;height:0;top:-10px;left:-10px;"; 119 iframe.setAttribute('style', 'position:absolute;width:0;height:0;top:-10px;left:-10px;'); 120 w = f.contentWindow || f.contentDocument; 121 doc = f.contentDocument || f.contentWindow.document; 122 doc.open(); 123 doc.write(content); 124 doc.close(); 125 var _this = this 126 iframe.onload = function(){ 127 _this.toPrint(w); 128 setTimeout(function () { 129 document.body.removeChild(iframe) 130 }, 100) 131 } 132 }, 133 134 toPrint: function (frameWindow) { 135 try { 136 setTimeout(function () { 137 frameWindow.focus(); 138 try { 139 if (!frameWindow.document.execCommand('print', false, null)) { 140 frameWindow.print(); 141 } 142 } catch (e) { 143 frameWindow.print(); 144 } 145 frameWindow.close(); 146 }, 10); 147 } catch (err) { 148 console.log('err', err); 149 } 150 }, 151 // 检查一个元素是否是 body 元素的后代元素且非 body 元素本身 152 isInBody: function (node) { 153 return (node === document.body) ? false : document.body.contains(node); 154 }, 155 isDOM: (typeof HTMLElement === 'object') ? 156 function (obj) { 157 return obj instanceof HTMLElement; 158 } : 159 function (obj) { 160 return obj && typeof obj === 'object' && obj.nodeType === 1 && typeof obj.nodeName === 'string'; 161 } 162 }; 163 const MyPlugin = {} 164 MyPlugin.install = function (Vue, options) { 165 // 4. 添加实例方法 166 Vue.prototype.$print = Print 167 } 168 export default MyPlugin
在 main.js 上 引入挂载到vue 上
import Print from '@/util/print'
Vue.use(Print)
这样就可以 使用了 this.$print(html)
页面中使用
<div ref="print"> <div> 需要打印的内容 </div> <div class="no-print"> 不需要打印的内容 </div> </div>
打印
this.$print(this.$refs.print)
用 ref="print" 包裹住要打印的范围, 其中不需要打印的模块添加类名 "no-print"