电子打印中构建的角度应用程序 div 显示空白窗口

Muh*_*lah 4 electron angular

我开发了一个角度应用程序,然后我在电子中构建了该应用程序。该应用程序工作正常,但是当我单击按钮打印特定的 div 时,它会打开一个电子的空白窗口。我用ngx-print图书馆。它适用于角度发球,但电子构建有问题。

<button class="btn btn-raised mr-1 shadow-z-2 btn-success"  
printSectionId="print-section" ngxPrint>
   print
</button>

<div id="print-section"> Print This</div>
Run Code Online (Sandbox Code Playgroud)

Fat*_*zli 6

在 ngx-print 代码中,我们有这部分来做到这一点:

printContents = document.getElementById(this.printSectionId).innerHTML;
popupWin = window.open('_blank', '', 'top=0,left=0,height=100%,width=auto');
popupWin.document.open();
popupWin.document.write("\n      <html>\n        <head>\n          <title>" + (this.printTitle ? this.printTitle : '') + "</title>\n          <style>\n            " + this.returnStyleValues() + "\n          </style>\n        </head>\n    <body onload=\"window.print();window.close()\">" + printContents + "</body>\n      </html>");
popupWin.document.close();
Run Code Online (Sandbox Code Playgroud)

当电子尝试打开新窗口时,它会打开一个BrowserWindowProxy,然后如果它尝试访问 popupWin.document ,它会得到未定义并显示错误。我们可以告诉电子打开一个本地窗口来访问它的打开和对文档的访问,所以在你的main.js或者main.ts你可以尝试:

win = new BrowserWindow({
    x: 0,
    y: 0,
    width: size.width,
    height: size.height,
    webPreferences: {
      nativeWindowOpen: true, // add this
      nodeIntegration: false
    }
 });
Run Code Online (Sandbox Code Playgroud)

然后电子将新建一个本地窗口。然后在 ngx-print 代码中

popupWin = window.open('', '_blank', 'top=0,left=0,height=100%,width=auto'); 
Run Code Online (Sandbox Code Playgroud)

应该:

popupWin = window.open('_blank', '', 'top=0,left=0,height=100%,width=auto');
Run Code Online (Sandbox Code Playgroud)

打开一个新的空白页。您可以webPreferenceshttps://electronjs.org/docs/api/browser-window 中阅读更多信息。