打字稿错误 TS1128:预期声明或语句



收到一个打字稿错误,我花了几个小时试图修复。它指向我的游戏详细信息.component.ts(26,54(,对我来说看起来完全没问题。

同样,当我在我的游戏细节.组件 webpack 中的 CTR + S 成功编译并且我的程序运行良好时。谁能向我解释为什么它会这样工作?

这是代码:

import { Component, OnInit, Input } from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router';
import { Location } from '@angular/common';
import { GameService } from '../../game.service';
import { UserComment } from '../../comment.model';
@Component({
  selector: 'app-game-detail',
  templateUrl: './game-detail.component.html',
  styleUrls: ['./game-detail.component.css'],
  providers: [GameService]
})
export class GameDetailComponent implements OnInit {
  gameKey: string;
  gameDetail;
  constructor(private route: ActivatedRoute, private location: Location, private gameService: GameService) { }
  ngOnInit() {
    this.route.params.forEach((urlParameters)=> {
      this.gameKey = urlParameters['id'];
    });
    this.gameService.getGameByKey(this.gameKey).subscribe(dataLastEmittedFromObserver => {
       this.gameDetail = dataLastEmittedFromObserver;);
      }
      addComment(com: string) {
      const newComment: UserComment = new UserComment(com);
      this.gameDetail.comments.push(newComment);
      this.gameService.updateComments(this.gameDetail);
      // this.toggleDisplay();
    }
}

如果你看一下你的代码,它在ngOnInit()内部有一个问题,你几乎没有错位的偏执,把它改成

ngOnInit() {
  this.route.params.forEach((urlParameters) => {
            this.gameKey = urlParameters['id'];
        });      
  this.gameService.getGameByKey(this.gameKey)
  .subscribe(dataLastEmittedFromObserver => {
            this.gameDetail = dataLastEmittedFromObserver;
        });
}

你所做的不是关闭括号之间的括号。 或者放错了括号。取决于你想做什么。

你放错了括号。

试试这个:

this.gameService.getGameByKey(this.gameKey).subscribe(dataLastEmittedFromObserver => {
        this.gameDetail = dataLastEmittedFromObserver;
       });

最新更新