我正在尝试编写一个服务来衡量应用程序变得稳定的完成时间。以下是该服务:
measure.service.ts
import { ApplicationRef, Injectable } from "@angular/core";
import { first, map } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class MeasureService {
public constructor(private readonly applicationRef: ApplicationRef) { }
public measure(): void {
const startTime = Date.now();
console.log(startTime);
/**
* Test stability metrics of application.
*/
this.applicationRef.isStable.pipe(
first(isStable => isStable),
map(() => {
/* tslint:disable-next-line:no-console */
console.log("App stable ("+ Math.round((Date.now() - startTime) / 1000) + " secs)");
})
);
}
}app.module.ts (缩写):
...
@NgModule({
providers: [
MeasureService
]
})root.component.ts
import { Component, OnInit } from "@angular/core";
import { MeasureService } from "../application-insights/measure.service";
@Component({
selector: "mr-root",
template: `
<router-outlet></router-outlet>
`,
})
export class RootComponent implements OnInit {
public constructor(
private readonly measureService: MeasureService
) {
this.measureService.measure();
}
public ngOnInit(): void {
}
}MeasureService的第一个console.log正确地记录了时间。但是,看起来applicationRef.isStable从不触发,因此,第二个console.log永远不会写入控制台。
我做得对吗,还是我漏掉了什么?
发布于 2019-11-14 04:21:54
measure.service.ts (更新):
import { ApplicationRef, Injectable } from "@angular/core";
import { first } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class MeasureService {
public constructor(private readonly applicationRef: ApplicationRef) { }
public measure(): void {
const startTime = Date.now();
/**
* Test stability metrics of application.
*/
this.applicationRef.isStable.pipe(
first(isStable => isStable)
).subscribe(() => {
/* tslint:disable-next-line:no-console */
console.log("App stable ("+ Math.round((Date.now() - startTime) / 1000) + " secs)");
});
}
}https://stackoverflow.com/questions/58844874
复制相似问题