【发布时间】:2019-02-05 22:22:05
【问题描述】:
我有一个带有打印按钮的组件。但是,当我从 test.component.css 打印 css 时,不包括在内。谁能帮我解决这个问题? 注意:我必须只在 test.component.css 中保留 css。我不想使用内联样式。
test.component.html:
<div class='container'>
<div>
<button (click)='printContent()' class="btn btn-default">
<span class="glyphicon glyphicon-print"></span> Print
</button>
</div>
<div id='content'>Hello World</div>
</div>
test.component.ts:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-test',
templateUrl: './test.component.html',
styleUrls: ['./test.component.css']
})
export class TestComponent implements OnInit {
constructor() { }
ngOnInit() {
}
printContent() {
let popupWin = window.open('', '_blank', 'width=1080,height=595');
let printContents = document.getElementById("content").innerHTML;
popupWin.document
.write('<html><head>' +
'<link rel="stylesheet" type="text/css" href="/test.component.css"/>' +
'</head><body onload="window.print();">'
+ printContents + '</body></html>');
popupWin.document.close();
}
}
test.component.css:
#content{
background-color: cyan;
font-weight: bold;
}
这是浏览器中的输出:
这是打印输出
【问题讨论】: