npm typescript -由于错误TS 1128,代码无法构建:应为声明或语句,但在提供代码时按预期运行

goucqfw6  于 2023-01-26  发布在  TypeScript
关注(0)|答案(5)|浏览(214)

我正在开发一个Angular2项目,并且创建了一个类作为主Component类:

import { Component, OnInit} from '@angular/core';

import { submitService } from './Common-functions/submit.service';

@Component({
selector: 'my-app',
template: `htmlCode`

})
export class AppComponent implements OnInit{ 
  hideTable = true;
  lengthOfRecords = 0;
  lowerLimit = 0;
  upperLimit = 5;
  prevButtonDisabled = true;
  nextButtonDisabled = false;

  //User inputs

constructor(private sService:submitService) { }

ngOnInit() {
    public submitToJSON() {

         //SumbitJSON Object
            var submitJSON = {
                //inputData 
                     };

            this.sService.POST(submitJSON);
     }

   public returnDetails() {

    this.listOfIDs = {//ListData};

            this.hideTable = false;
        var keys = Object.keys(this.listOfIDs);
            var len = keys.length;
            this.lengthOfRecords = len;
        }

    public prev() {
            if(this.lowerLimit <= 0) {
                ;
                }
            else {
                this.lowerLimit = this.lowerLimit - 6;
                this.upperLimit = this.upperLimit - 5;
                this.nextButtonDisabled = false;
                if(this.lowerLimit <= 0) {
                    this.prevButtonDisabled = true;
                    }
                }
            }
   public next() {
            if(this.upperLimit >= this.lengthOfRecords) {
                ;
                }
            else {
                this.lowerLimit = this.lowerLimit + 6;
                this.upperLimit = this.upperLimit + 5;
                this.prevButtonDisabled = false;
                if(this.upperLimit >= this.lengthOfRecords) {
                    this.nextButtonDisabled = true;
                    }
                }
            }

 getEntries(obj, from, to) {
        if(obj!=null) {
            var entries = [];
            for(var key in obj) {
                // extract index after `app`
                var index = key.substring(3);
                if(index >= from && index <= to) {
                    entries.push( obj[key]);
                }
            }
            return entries;
            }

 }

}

当我运行npm start(它将运行tsc -p ./)时,我得到以下两个错误:

app.appComponent.ts: error TS1128:Declaration or statement expected
app.appComponent.ts: error TS1128:Declaration or statement expected

在以下代码行中

---> public submitToJSON() {

     //SumbitJSON Object
        var submitJSON = {
            //inputData };

        this.sService.POST(submitJSON);
 }

在代码的最后一行。我已经修改了一整天的代码,只删除了OnInit相关的代码就修复了它。我做错了什么?我是Angular2的新手,所以任何信息都是有帮助的。我也在运行tsc版本3.1

yacmzcpb

yacmzcpb1#

在我的情况下,只是需要再次编译项目

aij0ehis

aij0ehis2#

刚刚有同样的错误,关闭IntelliJ并重新打开后就消失了。我一直在修改有错误的文件。就像它在看一个旧版本一样。

7d7tgy0s

7d7tgy0s3#

您已发表评论

//inputData };

我想花括号应该在下一行...

//inputData
};
    • 编辑**

ngOnInit函数不应包含其他函数:

import { Component, OnInit} from '@angular/core';

import { submitService } from './Common-functions/submit.service';

@Component({
selector: 'my-app',
template: `htmlCode`

})
export class AppComponent implements OnInit{ 
  hideTable = true;
  lengthOfRecords = 0;
  lowerLimit = 0;
  upperLimit = 5;
  prevButtonDisabled = true;
  nextButtonDisabled = false;

  //User inputs

constructor(private sService:submitService) { }

ngOnInit() {
    // Add any initialization code here
}

submitToJSON() {

     //SumbitJSON Object
        var submitJSON = {
            //inputData 
                 };

        this.sService.POST(submitJSON);
 }

returnDetails() {

this.listOfIDs = {//ListData};

        this.hideTable = false;
    var keys = Object.keys(this.listOfIDs);
        var len = keys.length;
        this.lengthOfRecords = len;
    }

prev() {
        if(this.lowerLimit <= 0) {
            ;
            }
        else {
            this.lowerLimit = this.lowerLimit - 6;
            this.upperLimit = this.upperLimit - 5;
            this.nextButtonDisabled = false;
            if(this.lowerLimit <= 0) {
                this.prevButtonDisabled = true;
                }
            }
        }
next() {
        if(this.upperLimit >= this.lengthOfRecords) {
            ;
            }
        else {
            this.lowerLimit = this.lowerLimit + 6;
            this.upperLimit = this.upperLimit + 5;
            this.prevButtonDisabled = false;
            if(this.upperLimit >= this.lengthOfRecords) {
                this.nextButtonDisabled = true;
                }
            }
        }

 getEntries(obj, from, to) {
    if(obj!=null) {
        var entries = [];
        for(var key in obj) {
            // extract index after `app`
            var index = key.substring(3);
            if(index >= from && index <= to) {
                entries.push( obj[key]);
            }
        }
        return entries;
        }


}
6jjcrrmo

6jjcrrmo4#

在Visual Studio代码中,我必须终止npm start,然后再次运行

npm run start
nsc4cvqm

nsc4cvqm5#

对于那些谁遇到的问题与短绒:我在WebStorm中经常遇到一些Linter错误永远坐在那里的情况。重新启动IDE就像马歇尔Thompson建议的那样工作,但是关闭文件标签并打开它也解决了这个问题。

相关问题