基本上是它的设置并获得一个数组。
import { Injectable } from '@angular/core';
@Injectable()
export class Svc {
constructor() {}
private _updatedArray: Array < any > = [];
setColDefs(columns: any) {
this._updatedArray.push(columns);
console.log(columns);
}
getColDef() {
return this._updatedArray;
}
}
在我的一个componnet中,我将数组设置如下:
...
this._listSvc.setColDefs(colDefArray);
...
在我的其他组件中,
...
import { Svc } from '../services/mySvc.svc';
@Component({
...
})
export class EditComponent implements OnInit, OnDestroy{
constructor(
private _editCompSvc: Svc
){}
ngOnInit() {
console.log(this._editCompSvc.getColDef());
}
但不知道为什么getColDef总是给出一个空列表。即使在我的服务中,getter函数也会返回一个空列表。伙计们好吗?非常感谢。
我的模块:
import { NgModule } from '@angular/core';
import { routing } from './routes/myroute.route';
import { Svc } from './services/mySvc.svc'
import { ListComponent } from './actions/list.comp';
import { EditComponent } from './actions/edit.comp';
@NgModule({
imports:
[
routing
],
declarations:
[
ListComponent,
EditComponent
],
providers:
[Svc]
})
export class Module { }
答案 0 :(得分:0)
您最有可能在EditComponent
ngOnInit() { // I'm sure this is happening before that update
console.log(this._editCompSvc.getColDef());
}
顺便说一句,写这篇文章更好:
export class Svc {
constructor() { }
private _updatedArray:Array<any> = [];
set colDefs(columns: any) {
this._updatedArray.push(columns);
console.log('setting the colDefs',columns);
}
get colDef(){
console.log('getting the colDefs',this._updatedArray);
return this._updatedArray;
}
要证明,运行上面的代码,您应该看到&#34;设置colDefs&#34;在得到它之前