我有一个带有角模板的Visual Studio 2017 .Net Core 2.0项目。当我在下面的" this.setuprefreshinterval()"线上删除时,Angular预处理时间。该应用返回错误" NodeInvocationException:在30000ms之后预定的预定时间,因为'clientapp/dist/dist/main-server'中的启动函数返回了一个未解决或拒绝的承诺。确保您的启动功能始终解决或拒绝其承诺。"有什么想法吗?
这是组件代码:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-currentdatetime',
templateUrl: './currentdatetime.component.html',
styleUrls: ['./currentdatetime.component.css']
})
export class CurrentDateTimeComponent implements OnInit {
private currentDateTime: Date;
constructor() {
this.refreshDate();
//this.setUpRefreshInterval();
}
get currentDateFormatted(): String {
return this.currentDateTime.toLocaleDateString();
};
get currentTimeFormatted(): String {
return this.currentDateTime.toLocaleTimeString();
};
ngOnInit(): void {
//this.setUpRefreshInterval();
}
private setUpRefreshInterval(): void {
setInterval(() => {
this.refreshDate();
}, 1000);
}
private refreshDate(): void {
this.currentDateTime = new Date();
}
}
我还尝试使用Observable.timer并获得相同的结果:
private setUpRefreshInterval(): void {
let timer = Observable.timer(0, 1000);
timer.subscribe(t => this.currentDateTime = new Date());
}
作为解决方法,我使用了克里希南努尼的建议,并做出了这种更改,它有效:
import { Component, OnInit, Inject } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';
import { PLATFORM_ID } from '@angular/core';
constructor(@Inject(PLATFORM_ID) private platformId: Object) {
this.refreshDate();
}
ngOnInit(): void {
// setting up the refresh interval caused a timeout in prerendering, so only set up interval if rendering in browser
if (isPlatformBrowser(this.platformId)) {
this.setUpRefreshInterval();
}
}
将间隔代码包裹在iSplatformbrowser条件下,以免预先渲染