One possible way to avoid server error is not to render the component(if it is an option) that uses window
. Something like:
<ng-container *ngIf="isBrowser">
<!-- mqttws31-component -->
<mqttws31-component></mqttws31-component>
</ng-container>
The isBrowser can be imported from(ng2)
import { isBrowser } from 'angular2-universal';
Or if ng4+, you can also define this in your browser module:
// app.browser
@NgModule({
providers: [
{ provide: 'isBrowser', useValue: true }
]
})
then inject from constructor
export class SomeComponent implements OnInit {
constructor(@Inject('isBrowser') private isBrowser: boolean)
ngOnInit() {
// example usage, this could be anywhere in this Component of course
if (this.isBrowser) {
alert('we're in the browser!');
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…