首页 > 解决方案 > PrimeNG TypeError "this._activeIndex.includes" 不是函数

问题描述

我有一个使用手风琴的组件。我希望启用多个选项卡和自定义 activeIndex。这是我的代码:

<p-accordion [activeIndex]="index" [multiple]="true">
  <p-accordionTab header="1st tab">
    1st tab content
  </p-accordionTab>
  <p-accordionTab header="2nd tab">
    2nd tab content
  </p-accordionTab>
</p-accordion>

这是我的组件类

@Component({
  selector: 'app-panel',
  templateUrl: './panel.component.html',
  styleUrls: ['./panel.component.css']
})
export class PanelComponent implements OnInit {

  index:number = 1;
  constructor() { 
  }


  ngOnInit() {
    //this.index = 0;
  }

}

如果我想同时包含 [activeIndex] 和 [multiple],就会出现问题。任何想法为什么会发生这种情况?

我使用 PrimeNG 7 和 angular 7

标签: angularprimengangular7

解决方案


文档说:

活动选项卡的索引或以编程方式更改选定选项卡的索引数组。

这是设置activeTabs的相关代码_activeIndex

 updateSelectionState() {
        if (this.tabs && this.tabs.length && this._activeIndex != null) {
            for (let i = 0; i < this.tabs.length; i++) {
                let selected = this.multiple ? this._activeIndex.includes(i) : (i === this._activeIndex);
                let changed = selected !== this.tabs[i].selected;

                if (changed) {
                    this.tabs[i].animating = true;
                }

                this.tabs[i].selected = selected;
                this.tabs[i].selectedChange.emit(selected);
            }
        }
    }

因此,对于多个选项卡,您应该使用索引数组,而不是数字。

@Component({
  selector: 'app-panel',
  templateUrl: './panel.component.html',
  styleUrls: ['./panel.component.css']
})
export class PanelComponent implements OnInit {

  indices: number[] = [1, 2];
  constructor() { 
  }


  ngOnInit() {
    //this.index = 0;
  }

}

如果您想知道,this._activeIndex.includes(i)这就是您的错误的来源。


推荐阅读