Javascript 如何在TypeScript函数中创建空数组,并在每次调用该函数时向其添加对象

Javascript 如何在TypeScript函数中创建空数组,并在每次调用该函数时向其添加对象,javascript,arrays,typescript,es6-class,Javascript,Arrays,Typescript,Es6 Class,使用TypeScript和Angular,我试图在testConnection函数中创建一个空数组,允许每次调用该函数时添加对象,而不清除数组 testConnection函数: testConnection(system) { var systemList = []; this.service.testConnection(system) .subscribe(conn => { if (conn == 'Success') {

使用TypeScript和Angular,我试图在testConnection函数中创建一个空数组,允许每次调用该函数时添加对象,而不清除数组

testConnection函数:

  testConnection(system) {
    var systemList = [];
    this.service.testConnection(system)
      .subscribe(conn => {
        if (conn == 'Success') {
          this.snackBarHandler.open('Connection Found', 'success');
          system.isClicked = false;
          system.connection = true;
          systemList.push(system);
        }
        else {
          this.snackBarHandler.open('Connection Failed', 'failure');
          system.isClicked = false;
          system.connection = false;
          systemList.push(system);
        }
      }, err => console.log(err));
  }
目前,使用我拥有的逻辑,它正在将系统对象添加到数组中,但由于空数组声明在函数中,因此每次调用函数时,它都会清除并重新启动此过程。我曾尝试在类的顶部声明systemList(systemList=any[]),但当我尝试在函数中引用它时,它显示为未定义


如何在调用函数时将系统对象添加到数组中,而不清除数组中的现有对象?

您应该能够在函数外部设置组件变量,然后列表将保持不变

export class SystemComponent implements OnInit {

  constructor(private service: TestConnectionService) {}

  systemList = []

  testConnection(system) {
    this.service.testConnection(system)
      .subscribe(conn => {
        this.systemList.push(system);
        if (conn === 'Success') {
          this.snackBarHandler.open('Connection Found', 'success');
          system.isClicked = false;
          system.connection = true;
        }
        else {
          this.snackBarHandler.open('Connection Failed', 'failure');
          system.isClicked = false;
          system.connection = false;
        }
      }, err => console.log(err));
  }
  ngOnInit(): void {}
}
另一个选项是创建一个服务,并使用该服务保存系统列表的状态,如果希望多个组件访问系统列表,该选项更有用

interface System {
  some: string
  props: string
}

@Injectable({ providedIn: 'root' })
export class SystemService {
  private _systemList$$ = new BehaviorSubject<System[]>([])


  get systemList(): System[] {
    return this._systemList$$.value
  }

  addToSystemList(system: System) {
    this._systemList$$.next([...this.systemList, system])
  }

  constructor() {}
}

如果您需要一个状态,该状态在一个函数的多个调用中保持不变,那么您必须在该函数之外定义它
systemList=any[]
不是有效的变量定义。请说明您是如何在类级别定义和使用systemList的。因此,换言之,这个问题应该是“我在何处存储有状态信息?”。我对此一无所知,但很明显你的函数做的太多了。尽量减少你的职责。它是维护有状态列表的函数吗?它是建立服务的功能吗?或者它是一个订阅服务抛出的“事件”并执行某些操作的函数?三项基本不同的职责,但只有一项职能。把事情分开。我不是一个棱角分明的人,但你不需要更多的打字稿吗<代码>系统列表:系统[]=[],
测试连接(系统:系统)
,等等。
testConnection(system) {
  this.service.testConnection(system).subscribe(
    conn => {
      this.systemService.addToSystemList(system)
      if (conn === 'Success') {
        this.snackBarHandler.open('Connection Found', 'success')
        system.isClicked = false
        system.connection = true
      } else {
        this.snackBarHandler.open('Connection Failed', 'failure')
        system.isClicked = false
        system.connection = false
      }
    },
    err => console.log(err)
  )
}