簡體   English   中英

angular typescript - 在網站上切換語言時顯示隱藏選項卡

[英]angular typescript - show hide tabs when switch language on website

當我在網站上切換語言時,我想顯示或隱藏一個選項卡,如果語言是德語則顯示它,如果任何其他語言則隱藏它,我的代碼:

ngOnInit(): void {
    this.translate.onLangChange.subscribe({next: (event: { lang: string; }) =>{
            if(event.lang && event.lang === 'de'){
                this.tabsArray =  this.tabsArray.filter((tab) => tab.label !== certainPathLabel );
            }
            else{
                const foundLabel = this.tabsArray.find((tab)=>tab.label == certainPathLabel );
                if(!foundLabel) {
                    this.tabsArray.splice(4,0, {label: certainPathLabel , route: this.routeService.generateUrl(certainPatRoute)})      
                }

            }
            this.tabs$.next(this.tabsArray);
        }

    });
}

這僅適用於我 select 德語,選項卡被隱藏但如果我然后 select 任何其他語言,無論我選擇什么我都無法再次顯示選項卡,有什么幫助嗎?

你有可能使用一個簡單的可觀察對象嗎?

isGerman$ = this.translate.onLangChange.pipe(
  map({ next: (event: { lang: string; }) } => // Are you sure for { } ?
    event.lang && event.lang === 'de'
  )
);
<tabs>
  <tab *ngIf="isGerman$ | async">German</tab>
</tabs>

暫無
暫無

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

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