簡體   English   中英

typescript observable 在頁面重新加載時不刷新

[英]typescript observable doesn't refresh on page reload

當我使用按鈕時,我有一個 angular 頁面。 它正確地寫了頁面標題。 不幸的是,當使用重新加載按鈕重新加載頁面時,標題不會被重寫。 這是 html 頁面的一部分:

header 部分的一部分:

 <div class="alternate-theme" color="primary" style="height:30px;">
    <span *ngIf="titleSite$ | async as title" class="center"> {{ title }} </span>
 </div>
 <mat-menu #menu="matMenu" [overlapTrigger]="false">
      <button mat-menu-item (click)="navigateMenu('home')">
        <mat-icon color="accent">home</mat-icon>
        <span>Home page</span>
      </button>
  </mat-menu>

.ts 文件的一部分:

export class TopMenuComponent implements OnInit {
  public titleSite$: Observable<string>;
}

navigateMenu(tag) {
   this.titleSite$ = this.translate.stream('Sitetitle0'); // Default title
   this.initialTag$ = tag;
   if (tag === "home") {
     this.titleSite$ = this.translate.stream('Sitetitle1');
     this.router.navigate(["/"]);
   }

因此,當單擊按鈕時,將寫入正確的標題。 但如果我重新加載頁面,它就是默認標題。

一切正常,除非有人點擊重新加載。

我怎樣才能強制使用正確的標題,? 以前在菜單中單擊的那個。

謝謝

每次點擊刷新都會破壞變量,因此當頁面加載時,初始值將始終是默認標題。

可能的解決方案:

  1. 使用本地/會話存儲來保存有關活動標題的信息,並在初始化時從那里讀取值。 如果它沒有 ecist 那么它就是默認標題。
  2. 將其存儲在服務器/外部某處

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM