首页 > 解决方案 > 错误 TS1005: ',' 预期,编译 Angular 6 项目失败

问题描述

每当我编译时,我都会收到此错误消息

ERROR in src/app/form/form.component.ts(22,39): error TS1005: ',' expected.

这是错误指向的完整代码:

import { Component, OnInit } from '@angular/core';
import { CommService } from '../services/comm.service';

@Component({
  selector: 'app-form',
  templateUrl: './form.component.html',
  styleUrls: ['./form.component.css']
})
export class FormComponent implements OnInit {


  user = {
    Doctor: '',
    Patient: '',
    Type: '',
    Diagnosis: ''
  };

  constructor(private CommService: CommService) { }

  ngOnInit() {
    this.CommService.setData(this.user);

  }

}

上面的代码是一个简单的形式,它接受输入(user={})并将其传递给服务(CommService.setData)

这是 CommService 代码:

import { Injectable } from '@angular/core';
import { Observable, of, Subject } from 'rxjs';
import { FormComponent } from '../form/form.component';

@Injectable()
export class CommService {

  getData$: Observable<any>;
  private getDataSubject = new Subject<string>();


  users = {
    Doctor: '',
    Patient: '',
    Type: '',
    Diagnosis: ''
  };

  constructor() { }

  setData (data: any[]) {
    this.users = data;
  }

  getData() {
    return this.users;
  }
}

我是一个初学者,所以我敢打赌我的错误很简单,如果你能帮助我,我将不胜感激

更新:几个聪明的家伙建议我检查“package.json”中的 typscript 版本,这是我发现的:`“typescript”:“~2.7.2”,所以我更新到 2.9,但不知何故我仍然有相同的错误!

另外,这是整个 package.json 文件

  {
  "name": "my-form",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^6.0.3",
    "@angular/common": "^6.0.3",
    "@angular/compiler": "^6.0.3",
    "@angular/core": "^6.0.3",
    "@angular/forms": "^6.0.3",
    "@angular/http": "^6.0.3",
    "@angular/platform-browser": "^6.0.3",
    "@angular/platform-browser-dynamic": "^6.0.3",
    "@angular/router": "^6.0.3",
    "core-js": "^2.5.4",
    "rxjs": "^6.0.0",
    "zone.js": "^0.8.26"
  },
  "devDependencies": {
    "@angular/compiler-cli": "^6.0.3",
    "@angular-devkit/build-angular": "~0.6.8",
    "typescript": "~2.7.2",
    "@angular/cli": "~6.0.8",
    "@angular/language-service": "^6.0.3",
    "@types/jasmine": "~2.8.6",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "codelyzer": "~4.2.1",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~1.7.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.0",
    "karma-jasmine": "~1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.3.0",
    "ts-node": "~5.0.1",
    "tslint": "~5.9.1"
  }
}

标签: javascriptangulartypescript

解决方案


检查您是否使用最新版本的打字稿,如果没有,请尝试https://stackoverflow.com/a/46399668/1817690

此外,of在旧版本rxjs中将无法使用,因此请在您的服务文件中进行更改

import { Observable, Subject } from 'rxjs';

并使用

import "rxjs/add/observable/of";

或者

import { of as observableOf } from 'rxjs/observable/of';

阅读typescript 中为 Observable 的 import .of()给出的答案


推荐阅读