简体   繁体   English

typescript observable 在页面重新加载时不刷新

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

I have an angular page that works when I use the button.当我使用按钮时,我有一个 angular 页面。 It writes the page title correctly.它正确地写了页面标题。 Unfortunately, when the page is reloaded with the reload button, the title is not rewritten.不幸的是,当使用重新加载按钮重新加载页面时,标题不会被重写。 Here's a part of the html page:这是 html 页面的一部分:

a part of the header portion: 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>

a portion of the.ts file: .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(["/"]);
   }

So when The button is clicked the right title is written.因此,当单击按钮时,将写入正确的标题。 But If I reload the page it's the default title.但如果我重新加载页面,它就是默认标题。

Everything is working fine, unless someone click the reload.一切正常,除非有人点击重新加载。

How can I force to use the right title, ?我怎样才能强制使用正确的标题,? the one that was previously click in the menu.以前在菜单中单击的那个。

Thanks谢谢

Each time you hit refresh you are destroying the variables, so when the page is loaded inital value will allways be default title.每次点击刷新都会破坏变量,因此当页面加载时,初始值将始终是默认标题。

Possible solutions:可能的解决方案:

  1. Use local/session storage to keep information about active title, and on init read the value from there.使用本地/会话存储来保存有关活动标题的信息,并在初始化时从那里读取值。 If it doesn't ecist then it is default title.如果它没有 ecist 那么它就是默认标题。
  2. Store it on server/somewhere externaly将其存储在服务器/外部某处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM