【问题标题】:Angular 2 Tabs retain after Refresh刷新后保留Angular 2选项卡
【发布时间】:2017-08-01 07:31:01
【问题描述】:

我在标签功能上工作正常,但我希望在页面刷新时保持选定的标签处于活动状态。

<tabs>
  <tab [tabTitle]="'Tab 1'">Tab 1 Content</tab>
  <tab tabTitle="Tab 2">Tab 2 Content</tab>
</tabs>

src/tabs.ts

import { Component, ContentChildren, QueryList, AfterContentInit } from '@angular/core';
import { Tab } from './tab';

@Component({
  selector: 'tabs',
  template:`
    <ul class="nav nav-tabs">
      <li *ngFor="let tab of tabs" (click)="selectTab(tab)" [class.active]="tab.active">
        <a href="#">{{tab.title}}</a>
      </li>
    </ul>
    <ng-content></ng-content>
  `
})
export class Tabs implements AfterContentInit {

  @ContentChildren(Tab) tabs: QueryList<Tab>;

  // contentChildren are set
  ngAfterContentInit() {
    // get all active tabs
    let activeTabs = this.tabs.filter((tab)=>tab.active);

    // if there is no active tab set, activate the first
    if(activeTabs.length === 0) {
      this.selectTab(this.tabs.first);
    } 
  }

  selectTab(tab: Tab){
    // deactivate all tabs
    this.tabs.toArray().forEach(tab => tab.active = false);

    // activate the tab the user has clicked on.
    tab.active = true;
  }

}

src/tab.ts

import { Component, Input } from '@angular/core';

@Component({
  selector: 'tab',
  styles: [`
    .pane{
      padding: 1em;
    }
  `],
  template: `
    <div [hidden]="!active" class="pane">
      <ng-content></ng-content>
    </div>
  `
})
export class Tab {
  @Input('tabTitle') title: string;
  @Input() active = false;
}

这是一个工作的笨蛋:

http://plnkr.co/edit/ctzfDxkGDWvnetWVRfwI?p=preview

【问题讨论】:

    标签: angular tabs angular2-template angular2-directives


    【解决方案1】:

    您只需要在selectTab 期间将选定的选项卡索引存储在存储中,并在初始化ngAfterContentInit 期间检查这一点。 对于此示例,我们将使用 localStorage。

    在 src/tabs.ts 中

    ngAfterContentInit() {
        // get all active tabs
        let activeTabs = this.tabs.filter((tab)=>tab.active);
        //get the active tab from storage,
        //if there are no stored, first tab is selected
        let selectedIndex: number =  parseInt(localStorage.getItem('selectedTabIndex'))  || 0;
        this.selectTab(this.tabs.toArray()[selectedIndex]);
    
    }
    
    selectTab(tab: Tab){
        // deactivate all tabs
        this.tabs.toArray().forEach(tab => tab.active = false);
        //store selected tab index in localStorage
        let selectedIndex: number = this.tabs.toArray().indexOf(tab);
        localStorage.setItem('selectedTabIndex',selectedIndex);
        // activate the tab the user has clicked on.
        tab.active = true;
    }
    

    工作人员:http://plnkr.co/edit/4ZkcBulnpqYcxjtQgXuG?p=preview

    【讨论】:

    • 在用我的实际项目实现 selectedIndex 时遇到一个问题“数字”类型的参数不可分配给“字符串”类型的参数。
    • 我尝试添加 type selectedIndex : any;但不工作
    • 当然,因为 localstorage 可能会返回一个字符串,请尝试使用 parseInt() 或 +var,我更新了我的答案
    • @Deepak 不需要另存为 JSON.stringify 它是一个数字,a number.toString() 可能会有所帮助,但它不是必需的。您遇到的错误是通过获取值而不是通过分配:) 所以只是let selectedIndex: number = parseInt(localStorage.getItem('selectedTabIndex')) || 0; parseInt 是关键
    • 好吧,无论如何它都可以满足我的要求,感谢您的快速支持。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-12
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    相关资源
    最近更新 更多