import { Component, OnInit } from '@angular/core'; import { Router, ActivatedRoute, Params } from '@angular/router'; import { System, Table } from './shared/platform.model'; import { PlatformService } from './shared/platform.service'; @Component({ selector: 'plat-form', templateUrl: './platform.component.html', }) export class PlatformComponent implements OnInit { isCollapsed = false; loading = false; systems: System[]; currentSystem: System; systemId: number; tables: Table[]; currentTable: Table; constructor(private router: Router, private routeInfo: ActivatedRoute, private platformServ: PlatformService) { } ngOnInit() { const a = 1; // 监测路由 // this.routeInfo.paramMap.subscribe(pmap => this.getSystems()); this.getSystems(); } getSystems() { this.loading = true; this.platformServ.getSystemList().subscribe(result => { this.systems = result.data; this.currentSystem = this.systems.find(item => item.id == this.routeInfo.snapshot.params.systemId); // id不存在就赋值第一项的id if (this.currentSystem === undefined) { this.currentSystem = this.systems[0]; this.router.navigate(['/platform', this.currentSystem.id, 0, 0]); // return; } this.getTables(this.currentSystem.id); }); } getTables(systemId: number) { this.loading = true; this.platformServ.getTableList(systemId).subscribe(result => { this.tables = result.data; // 路由tableID是否存在 this.currentTable = this.tables.find(item => item.id == this.routeInfo.snapshot.params.tableId); // id不存在就赋值第一项的id if (this.currentTable === undefined) { this.currentTable = this.tables[0]; this.router.navigate(['/platform', systemId, this.currentTable.id, 0]); } this.loading = false; }); } }