admin管理员组

文章数量:1391934

Maybe it render, that took xxMS. What happen? Anytime I close the print page, I receive this message in the chrome console. Does anyone fix this problem?

<button @click="printScreen()" type="button">print</button>

<div ref="printparts">test</div>

    methods: {
          printScreen() {
            let value = this.$refs.printparts;
            let printPage = window.open();
            printPage.focus();
            printPage.document.body.insertAdjacentHTML('afterbegin', value.outerHTML);
            printPage.print();
            printPage.close();
          },
        },

Maybe it render, that took xxMS. What happen? Anytime I close the print page, I receive this message in the chrome console. Does anyone fix this problem?

<button @click="printScreen()" type="button">print</button>

<div ref="printparts">test</div>

    methods: {
          printScreen() {
            let value = this.$refs.printparts;
            let printPage = window.open();
            printPage.focus();
            printPage.document.body.insertAdjacentHTML('afterbegin', value.outerHTML);
            printPage.print();
            printPage.close();
          },
        },
Share Improve this question edited Mar 1, 2020 at 4:48 Tom asked Mar 1, 2020 at 4:30 TomTom 211 silver badge3 bronze badges 2
  • Can you also please show us the code for the button and for the event listener? – Sydney Y Commented Mar 1, 2020 at 4:32
  • Sorry, I have edit my question, please review on it. Thanks – Tom Commented Mar 1, 2020 at 4:50
Add a ment  | 

2 Answers 2

Reset to default 4

The reason why you are getting this violation warning is likely because the event handler doesn't return until the print page is closed. So, when you click the button the print page opens, then nothing happens until the print page is closed, then the function returns.

I used onmouseover and Chrome doesn't show the violation, but Performance say Warning.

printScreen() {
        let value = this.$refs.printparts;
        let printPage = window.open();
        printPage.focus();
        printPage.document.body.insertAdjacentHTML('afterbegin', value.outerHTML);
        printPage.onmouseover = function() {
          printPage.print();
          printPage.close();
        };
        return false;
      },

本文标签: javascriptquotViolation 39click39 handler took 43665msquot in vuejsStack Overflow