函数实现丢失或不是紧跟在声明TypeScript类之后 [英] Function implementation is missing or not immediately following the declaration, TypeScript class

查看:1237
本文介绍了函数实现丢失或不是紧跟在声明TypeScript类之后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个手写的数组来填充班级中的表格,现在我正在获取该数组的 内容来自ngOnInit上的JSON,但未按我的要求进行结构化.

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上的这个新函数填充表数组.

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

问题是,当我在TS类的函数外部编写代码时,出现此错误函数实现丢失或在声明后不立即执行".

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".

为什么会这样?该怎么办才能解决此问题?

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

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

推荐答案

此处的问题是,您在可执行区域"(例如ngOnInit).

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);以便查看devtools中的数据,则应将console.log部分移到ngOnInit内,该部分是类MyComponent的可执行部分,或者也许在constructor.

If you need to do console.log(this.users); in order to see the data in the devtools, you should move the console.log part inside the ngOnInit which is an executable part of you class MyComponent or maybe inside the 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执行的某些方法中.

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

请参见 此演示 和一些示例.相关代码如下:

See this demo with 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
}

这篇关于函数实现丢失或不是紧跟在声明TypeScript类之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆