Angular 布线时有角度的2个通过对象

Angular 布线时有角度的2个通过对象,angular,oop,typescript,routes,Angular,Oop,Typescript,Routes,我试图在路由时在页面之间传递一个对象(数组)。为此,我完全照我说的做了,但这对我不起作用 服务 @Injectable () export class ReportService extends HttpService { public selectedReports: any[] = []; public setSelectedReports (id: string, value: any) { this.selectedReports[id] = valu

我试图在路由时在页面之间传递一个对象(数组)。为此,我完全照我说的做了,但这对我不起作用

服务

@Injectable ()
export class ReportService extends HttpService {

    public selectedReports: any[] = [];

    public setSelectedReports (id: string, value: any) {
        this.selectedReports[id] = value;
    }

    public removeSelectedReports (id: string) {
         delete this.selectedReports[id];
    }
}
家长

import { ReportService } from './';

@Component({
  providers: [ReportService]
})

export class ReportComponent {
  constructor (private reportService: ReportService) {}
}
儿童1

import { ReportService } from '../';
@Component({
  template: '<a [routerLink]="['stats']">Stats</a>'
})

export class ReportHomeComponent {

  constructor (private reportService: ReportService) {
    reportService.setSelectedReports (1, 'hello')
  }
}
如果我在第一个子项中单击
a
,我会被重定向到第二个子项。在更改页面之前,将填写
selectedReports[]
。更改页面后,它是空的。我错过什么了吗


我知道以前有人问过这个问题,但我还是决定在问题顶部的链接中给出的答案的评论部分中根据要求问这个问题。

您可能以两种不同的方式导入服务。在您正在使用的父组件中:

@Component({
  providers: [ReportService]  //<--unique instance injected into this component
})

我已替换模块的提供程序,但
selectedReports
仍然为空。父节点的模块现在是提供它的唯一位置。在我看来,如果服务在修改数组后提供空数组,则它或服务在路由更改后再次被构造。如果删除
=[]
并仅在
setSelectedReports
中创建未定义的数组,会发生什么情况?
selectedReports
在加载新页面时未定义。。。。我在我的一个应用程序中的几个地方做同样的事情,没有任何问题。服务被创建了不止一次,或者有什么东西正在清除它,例如重新启动应用程序的整个页面刷新。我在
导入
@Injectable()
之间放置了一个
控制台。重定向到第二页时,找不到日志。
@Component({
  providers: [ReportService]  //<--unique instance injected into this component
})
@NgModule({
  imports: [
    ...
  ],
  declarations: [
    ...
  ],
  providers: [ReportService],  //<--inject service here to be global to module
  bootstrap: [AppComponent]
})