首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ApplicationRef.isStable未执行

ApplicationRef.isStable未执行
EN

Stack Overflow用户
提问于 2019-11-14 04:12:11
回答 1查看 541关注 0票数 1

我正在尝试编写一个服务来衡量应用程序变得稳定的完成时间。以下是该服务:

measure.service.ts

代码语言:javascript
复制
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 (缩写):

代码语言:javascript
复制
...
@NgModule({
   providers: [
      MeasureService
   ]
})

root.component.ts

代码语言:javascript
复制
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永远不会写入控制台。

我做得对吗,还是我漏掉了什么?

EN

回答 1

Stack Overflow用户

发布于 2019-11-14 04:21:54

measure.service.ts (更新):

代码语言:javascript
复制
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)");
    });
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58844874

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档