import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core'; import * as _ from 'lodash'; @Component({ selector: 'tab-alt', template: `

{{tab.name}}

` }) export class TabAlternativesComponent implements OnInit { @Input() alts; // 所有项 @Input() index; // 当前打开的tab在HomeComponent.tabs的索引HomeComponent.tabs[index.1][index.0] @Output() tabSelect = new EventEmitter(); alternatives = []; // 扩展了selected属性的所有可供选择项 constructor() { } ngOnInit(): void { if (this.alts) { this.alts.forEach(val => { this.alternatives.push(_.extend({selected: false}, val)); }); } } selectTab(tab) { this.alternatives.forEach(value => { value.selected = false; }); tab.selected = true; this.tabSelect.emit({ index: this.index, tab: _.omit(tab, ['selected']) }); } }