【发布时间】:2023-03-31 04:01:01
【问题描述】:
我有一个手写数组来填充我班级的一张桌子,现在我得到了这个数组 来自 ngOnInit 上的 JSON 的内容,但它的结构不是我需要的。
所以我正在尝试编写一个函数来用我在 ngOnInit 上得到的这个新数组填充表格数组。
问题是,当我在我的 TS 类中的函数之外编写代码时,我收到此错误“函数实现丢失或未立即在声明之后”。
为什么会这样?可以做些什么来解决这个问题?
TS
export class MyComponent implements OnInit {
users: Object;
constructor(private tstService: MyComponentService) { this.source = new LocalDataSource(this.data) }
ngOnInit(): void {
this.tstService.getTstWithObservable()
.map(result => result.map(i => i.user.data))
.subscribe(
res => { this.users = res; }
);
}
console.log(this.users); // Here, just an example. Throws 'Function implementation is missing or not immediately following the declaration'
data = [
{
title: 'Monthly',
sdate: '01/04/1990',
edate: '30/09/1990',
},
];
source: LocalDataSource;
}
【问题讨论】:
-
认为解释已经足够了。刚刚编辑了OP。问题是我在函数或 ngOnInit 之外编写的任何内容都会引发上述错误。
-
尝试在构造函数(数据和源)之前声明变量。
标签: angular function typescript class declaration