函数实现丢失或没有紧跟在声明之后,TypeScript class
Function implementation is missing or not immediately following the declaration, TypeScript class
我有一个手写数组来填充我的class中的一个table,现在我得到这个数组的
来自 ngOnInit 的 JSON 的内容,但它的结构不符合我的需要。
所以我正在尝试编写一个函数来用我正在使用 ngOnInit 的这个新数组填充 table 数组。
问题是,当我在我的 TS class 中的函数之外编写代码时,出现此错误 "Function implementation is missing or not immediately following the declaration"。
为什么会这样,可以采取什么措施来解决这个问题?
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;
}
这里的问题是 "executable area" 外有一些 "code execution" (console.log(this.users);
)(例如 ngOnInit
内的 "area")。
如果您需要执行 console.log(this.users);
以便在 devtools 中查看数据,您应该将 console.log
部分移动到 ngOnInit
中,这是您的可执行部分 class MyComponent
或者可能在 constructor
.
里面
我建议你这样做:
ngOnInit(): void {
this.tstService.getTstWithObservable()
.map(result => result.map(i => i.user.data))
.subscribe(
res => {
this.users = res;
console.log(this.users); // <-- moved here!
}
);
}
问题是您要执行的代码需要在 Angular 执行的某个方法中。
参见 this demo 中的一些示例。相关代码如下:
export class AppComponent implements OnInit{
name = 'Angular 6';
constructor() {
console.log(name); // OK
}
ngOnInit() {
console.log('sample not giving error'); // OK
}
// comment line below and the error will go away
console.log(name); // this will throw: Function implementation is missing or not immediately following the declaration
}
我有一个手写数组来填充我的class中的一个table,现在我得到这个数组的 来自 ngOnInit 的 JSON 的内容,但它的结构不符合我的需要。
所以我正在尝试编写一个函数来用我正在使用 ngOnInit 的这个新数组填充 table 数组。
问题是,当我在我的 TS class 中的函数之外编写代码时,出现此错误 "Function implementation is missing or not immediately following the declaration"。
为什么会这样,可以采取什么措施来解决这个问题?
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;
}
这里的问题是 "executable area" 外有一些 "code execution" (console.log(this.users);
)(例如 ngOnInit
内的 "area")。
如果您需要执行 console.log(this.users);
以便在 devtools 中查看数据,您应该将 console.log
部分移动到 ngOnInit
中,这是您的可执行部分 class MyComponent
或者可能在 constructor
.
我建议你这样做:
ngOnInit(): void {
this.tstService.getTstWithObservable()
.map(result => result.map(i => i.user.data))
.subscribe(
res => {
this.users = res;
console.log(this.users); // <-- moved here!
}
);
}
问题是您要执行的代码需要在 Angular 执行的某个方法中。
参见 this demo 中的一些示例。相关代码如下:
export class AppComponent implements OnInit{
name = 'Angular 6';
constructor() {
console.log(name); // OK
}
ngOnInit() {
console.log('sample not giving error'); // OK
}
// comment line below and the error will go away
console.log(name); // this will throw: Function implementation is missing or not immediately following the declaration
}