最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Angular?服務(wù)器端渲染應(yīng)用常見的內(nèi)存泄漏問題小結(jié)

 更新時(shí)間:2022年06月23日 10:33:46   作者:JerryWang_sap  
這篇文章主要介紹了Angular?服務(wù)器端渲染應(yīng)用一個(gè)常見的內(nèi)存泄漏問題,主要包括屏幕閃爍問題,出現(xiàn)閃爍的原因,在于 Angular 不知道如何重用它在服務(wù)器上成功渲染的內(nèi)容,本文給大家介紹的非常詳細(xì),需要的朋友一起學(xué)習(xí)下吧

考慮如下的 Angular 代碼:

import { Injectable, NgZone } from "@angular/core";
import { interval } from "rxjs";
@Injectable()
export class LocationService {
  constructor(ngZone: NgZone) {
    ngZone.runOutsideAngular(() => interval(1000).subscribe(() => {
      ...
    }));
  }
}

這段代碼不會影響應(yīng)用程序的穩(wěn)定性,但是如果應(yīng)用程序在服務(wù)器上被銷毀,傳遞給訂閱的回調(diào)將繼續(xù)被調(diào)用。 服務(wù)器上應(yīng)用程序的每次啟動(dòng)都會以 interval 的形式留下一個(gè) artifact.

這是一個(gè)潛在的內(nèi)存泄漏點(diǎn)。

這個(gè)內(nèi)存泄漏風(fēng)險(xiǎn)可以通過使用 ngOnDestoroy 鉤子解決。這個(gè)鉤子適用于 Component 和 service. 我們需要保存 interval 返回的訂閱(subscription),并在服務(wù)被銷毀時(shí)終止它。

退訂 subscription 的技巧有很多,下面是一個(gè)例子:

import { Injectable, NgZone, OnDestroy } from "@angular/core";
import { interval, Subscription } from "rxjs";
@Injectable()
export class LocationService implements OnDestroy {
  private subscription: Subscription;

  constructor(ngZone: NgZone) {
    this.subscription = ngZone.runOutsideAngular(() =>
      interval(1000).subscribe(() => {})
    );
  }
  ngOnDestroy(): void {
    this.subscription.unsubscribe();
  }
}

屏幕閃爍問題

用戶的瀏覽器顯示從服務(wù)器渲染并返回的頁面,一瞬間出現(xiàn)白屏,閃爍片刻,然后應(yīng)用程序開始運(yùn)行,看起來一切正常。出現(xiàn)閃爍的原因,在于 Angular 不知道如何重用它在服務(wù)器上成功渲染的內(nèi)容。在客戶端環(huán)境中,它從根元素中 strip 所有 HTML 并重新開始繪制。

閃爍問題可以抽象成如下步驟:

關(guān)于正在發(fā)生的事情的一個(gè)非常簡化的解釋:

(1) 用戶訪問應(yīng)用程序(或刷新)

(2) 服務(wù)器在服務(wù)器中構(gòu)建html

(3) 它被發(fā)送到用戶的瀏覽器端

(4) Angular 重新創(chuàng)建 應(yīng)用程序(就好像它是一個(gè)常規(guī)的非 Angular Universal 程序)

(5) 當(dāng)上述四個(gè)步驟發(fā)生時(shí),用戶會看到一個(gè) blink 即閃爍的屏幕。

代碼如下:

// You can see this by adding:
// You should see a console log in the server
// `Running on the server with appId=my-app-id`
// and then you'll see in the browser console something like
// `Running on the browser with appId=my-app-id`
export class AppModule {
  constructor(
    @Inject(PLATFORM_ID) private platformId: Object,
    @Inject(APP_ID) private appId: string) {
    const platform = isPlatformBrowser(this.platformId) ?
      'in the browser' : 'on the server';
    console.log(`Running ${platform} with appId=${this.appId}`);
  }
}

無法通過 API 的方式終止渲染

什么時(shí)候需要人為干預(yù)的方式終止一個(gè)服務(wù)器端渲染?

始終明確一點(diǎn),渲染應(yīng)用程序的時(shí)間點(diǎn)發(fā)生在應(yīng)用程序 applicationRef.isStable 返回 true 時(shí),參考下列代碼:

github.com/angular/ang…

function _render<T>(
    platform: PlatformRef, moduleRefPromise: Promise<NgModuleRef<T>>): Promise<string> {
  return moduleRefPromise.then((moduleRef) => {
    const transitionId = moduleRef.injector.get(?TRANSITION_ID, null);
    if (!transitionId) {
      throw new Error(
          `renderModule[Factory]() requires the use of BrowserModule.withServerTransition() to ensure
the server-rendered app can be properly bootstrapped into a client app.`);
    }
    const applicationRef: ApplicationRef = moduleRef.injector.get(ApplicationRef);
    return applicationRef.isStable.pipe((first((isStable: boolean) => isStable)))
        .toPromise()
        .then(() => {
          const platformState = platform.injector.get(PlatformState);
         ...

到此這篇關(guān)于Angular 服務(wù)器端渲染應(yīng)用一個(gè)常見的內(nèi)存泄漏問題的文章就介紹到這了,更多相關(guān)Angular內(nèi)存泄漏內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

岳阳市| 武宁县| 呼图壁县| 邯郸县| 建始县| 疏勒县| 达拉特旗| 浦江县| 成安县| 招远市| 庄河市| 平凉市| 东平县| 丁青县| 英山县| 宕昌县| 开化县| 峨眉山市| 高清| 卢氏县| 丹东市| 丽水市| 永昌县| 托里县| 罗定市| 阳信县| 如皋市| 嘉鱼县| 武安市| 永平县| 新营市| 阆中市| 临海市| 高雄县| 广河县| 赤城县| 彭州市| 玛多县| 万源市| 东辽县| 尖扎县|