函数实现缺失或没有紧跟在声明之后,TypeScript 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51266438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:31:48  来源:igfitidea点击:

Function implementation is missing or not immediately following the declaration, TypeScript class

angularfunctiontypescriptclassdeclaration

提问by prevox

I got a handwritten array to fill a table in my class, now I'm getting this array's content from a JSON on ngOnInit but it's not structured as I need.

我有一个手写数组来填充我的班级中的表格,现在我从 ngOnInit 上的 JSON 获取这个数组的内容,但它的结构不是我需要的。

So I'm trying to write a function to fill the table array with this new one I'm getting on ngOnInit.

所以我正在尝试编写一个函数,用我在 ngOnInit 上得到的这个新函数来填充表数组。

The issue is that when I write code outside of a function in my TS class I get this error "Function implementation is missing or not immediately following the declaration".

问题是,当我在我的 TS 类中的函数之外编写代码时,我收到此错误“函数实现丢失或未立即跟在声明之后”。

Why is that and what can be done to fix this?

为什么会这样,可以做些什么来解决这个问题?

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;
}

回答by lealceldeiro

The issue here is that you have some "code execution" (console.log(this.users);) outside an "executable area" (for instance the "area" inside the ngOnInit).

这里的问题是您console.log(this.users);在“可执行区域”(例如 内部的“区域”)之外有一些“代码执行”(ngOnInit)。

If you need to do console.log(this.users);in order to see the data in the devtools, you should move the console.logpart inside the ngOnInitwhich is an executable part of you class MyComponentor maybe inside the constructor.

如果您需要这样做console.log(this.users);以查看 devtools 中的数据,您应该将console.log部分移动到ngOnInit您的类的可执行部分中,MyComponent或者移动到constructor.

I would recommend you to do it like this:

我建议你这样做:

ngOnInit(): void {
    this.tstService.getTstWithObservable()
    .map(result => result.map(i => i.user.data))
    .subscribe(
       res => {
                this.users = res;
                console.log(this.users); // <-- moved here!
       }
   );
}

The thing is the code you're trying to execute needs to be inside some method which Angular executes.

问题是您尝试执行的代码需要在 Angular 执行的某个方法中。

See this demowith some examples. The relevant code is below:

请参阅此演示并附上一些示例。相关代码如下:

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
}