【问题标题】:How to hide element in Vue-html-to-paper?如何在 Vue-html-to-paper 中隐藏元素?
【发布时间】:2020-07-22 08:41:49
【问题描述】:

当我打印页面时,我需要隐藏一些东西。当我想打印时,不想显示 2 个按钮标志。我用插件https://www.npmjs.com/package/vue-html-to-paper

检查我的代码:

<div id="printThis">
 <h1> My tiyle </h1>
 <button> I WAN'T TO DISPLAY THIS BUTTON IN PRINT!!! </button>
 <p> My parag </p>
</div>


print(){
  this.$htmlToPaper('printThis');
}

问题很简单。我不想在我的 pdf 论文中显示按钮。

【问题讨论】:

    标签: javascript vue.js plugins


    【解决方案1】:

    我已经阅读了https://github.com/mycurelabs/vue-html-to-paper/blob/master/src/index.js 的源代码,看起来并没有内置变量来执行此操作(确实应该有,也许您可​​以提出拉取请求?)。但我们可以自己制作。

    在组件的data 部分中,添加一个名为printing 的变量:

      data() {
        return {
          printing: false,
        }
      },
    

    编辑您的模板以使用它:

    <template>
    <div id="printThis">
     <h1> My tiyle </h1>
     <button v-show="!printing"> I WAN'T TO DISPLAY THIS BUTTON IN PRINT!!! </button>
     <p> My parag </p>
    </div>
    </template>
    

    (请注意,您也可以使用 v-if。有一些优点和一些缺点。)

    然后将插件的方法调用包装在您自己的方法中以设置和取消设置变量。使用插件文档描述的回调来取消设置变量:

    print(){
      this.printing = true;
      const options = null; // Set this if you need to, based on https://randomcodetips.com/vue-html-to-paper/
      this.$htmlToPaper('printThis', options, () => {
        this.printing = false;
      });
    }
    

    你去吧。

    我会说,您可能可以编写一个更好的插件来做同样的事情。最好有一个基于承诺的方法,或者至少为打印成功或失败定义一些生命周期事件。

    【讨论】:

    • 你能解释一下什么不起作用以便我帮忙吗?
    • 你的方法 this.printing = true;常量选项=空; // 如果需要,根据randomcodetips.com/vue-html-to-paper this.$htmlToPaper('printThis', options, () => { this.printing = false; });在此之后,我看不到我的打印元素。
    • 检查我对管理员这个插件的回答...github.com/mycurelabs/vue-html-to-paper/issues/…
    • 对不起,我需要更多细节才能继续……图书馆的维护者甚至给出了与我相同的答案。我们需要更多细节来了解为什么它不适合您。
    猜你喜欢
    • 2021-05-09
    • 2020-12-22
    • 1970-01-01
    • 2019-06-11
    • 2011-12-04
    • 1970-01-01
    • 2021-04-05
    • 2011-12-28
    • 2017-04-06
    相关资源
    最近更新 更多