我正在尝试在我的 Angular 应用程序中实现打印功能。这是我到目前为止所做的:
typescript
print() {
let printContents, popupWin;
printContents = document.getElementById('print-section').innerHTML;
popupWin = window.open();
popupWin.document.open();
popupWin.document.write(`
<html>
<head>
<link rel="stylesheet" type="text/css" href="print.component.css">
</head>
<body onload="window.print();window.close()">${printContents}</body>
</html>`
);
popupWin.document.close();
}
html
<div id="print-section" hidden>
<div class="red">Hello</div>
</div>
CSS
.red{
color:red;
}
打印的东西有效,但它不加载 css。如果我添加 <style>
write()
内的标签它起作用的功能。为什么没有加载 css? .css
文件和 .ts
文件在同一个文件夹中。
编辑:感谢@match 的建议,我意识到浏览器在当前路径中查找 css 文件,即 www.myurl.it/currentpage/print.component.css。相反,我希望浏览器在源文件夹中查找文件,即 coponents/myComponent。有办法吗?
最佳答案
使用
@media print {
.red{
color:red;
}
}
关于css - Angular - 打印功能不加载 css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48864037/