javascript - 如何在模态 vue 中使用窗口打印?

标签 javascript vue.js vuejs2 bootstrap-modal vue-component

我的 vue 组件是这样的:

<template>
    ...
    <b-modal id="modalInvoice" size="lg"  title="Invoice">
        <Invoice/>
        <div slot="modal-footer" class="w-100"> 
            <b-btn size="sm" class="float-right" variant="warning" @click="show=false">
                <i class="fa fa-print"></i> Print
            </b-btn>
        </div>
    </b-modal>

    ...
        <b-btn variant="warning" class="btn-square mt-2" v-b-modal.modalInvoice @click="checkout()"><i class="fa fa-credit-card-alt"></i>Checkout</b-btn>
    ...
</template>
<script>
    ...
    export default {
        ...
        methods: {
            print() {
                window.print()
            }
        }
    }
</script>

如果我点击打印按钮,它就会工作。但它显示了整个网站。我只想点击打印按钮,它只显示模式的内容

我该怎么做?

最佳答案

实现此目的的一种方法是创建模式的克隆并使用 css 来限制打印内容的可见性:

print () {
  const modal = document.getElementById("modalInvoice")
  const cloned = modal.cloneNode(true)
  let section = document.getElementById("print")

  if (!section) {
     section  = document.createElement("div")
     section.id = "print"
     document.body.appendChild(section)
  }

  section.innerHTML = "";
  section.appendChild(cloned);
  window.print();
}

@media screen {
  #print {
    display: none;
   }
}

@media print {
 body * {
  visibility:hidden;
  }
  #print, #print * {
    visibility:visible;
  }
  #print {
    position:absolute;
    left:0;
    top:0;
  }
}

关于javascript - 如何在模态 vue 中使用窗口打印?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52278216/

相关文章:

JavaScript 用一张图像填充浏览器

vue.js - @nuxtjs/auth 为什么刷新页面总是重定向到登录

vue.js - Vuetify 主题不变

javascript - Vue 组件上的条件根标记

vuejs2 - 如何在 VueJs 中实现脏状态

javascript - JQuery 列表追加项目不可选择

javascript - Angular - 通过 ng-options 为选择字段绑定(bind)以下数据

javascript - Facebook 使用 GWT 打开图表

javascript - 为什么 v-for 只能工作一瞬间?

vue.js - Vue 可拖动元素带有点击事件?