简体   繁体   English

错误 TS1005: ',' expected, failing to compile angular 6 project

[英]error TS1005: ',' expected, failing to compile angular 6 project

whenever i compile, i get this error msg每当我编译时,我都会收到此错误消息

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

here is the entire code where the error points at:这是错误指向的整个代码:

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

  }

}

the above code is a simple form, it takes input (user={}) and passes it to a service (CommService.setData)上面的代码是一个简单的形式,它接受输入 (user={}) 并将其传递给服务 (CommService.setData)

here is the CommService code:这是通讯服务代码:

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

i am quite a beginner so i bet my mistake is simple, if u can help me i would be grateful我是一个初学者,所以我敢打赌我的错误很简单,如果你能帮助我,我将不胜感激

UPDATE: a couple of smart fellas suggested i check the typscript version in the "package.json" here is what i found: `"typescript": "~2.7.2", so i updated to 2.9 but somehow i still have the same error!更新:一些聪明的家伙建议我检查“package.json”中的打字稿版本这是我发现的:`“打字稿”:“〜2.7.2”,所以我更新到2.9但不知何故我仍然有相同的错误!

also, here is the entire package.json file另外,这是整个 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"
  }
}

问题是我刚刚更新到最新的过时的打字稿版本

Coming to this a more than four years later, maybe will help someone maintaining a legacy Angular application.四年多后,也许会帮助某人维护遗留 Angular 应用程序。

Upgrading Typescript proved to be a futile exercise.升级 Typescript 被证明是徒劳的。 The project pins v2.7.2 in the devDependencies .该项目将 v2.7.2 固定在devDependencies中。 I upgraded Typescript though v2.9.x` (moving beyond v2.9.x, eg, v2.10.0, ran into Angular 6 Typescript support errors).我通过 v2.9.x 升级了 Typescript(超越 v2.9.x,例如 v2.10.0,遇到了 Angular 6 Typescript 支持错误)。

I could see that the TS error was coming from minimatch 5.1.x .我可以看到 TS 错误来自minimatch 5.1.x I quickly determined the dependency tree pulling this in using npm ls @types/minimatch我很快确定了使用npm ls @types/minimatch拉动它的依赖树

% npm ls @types/minimatch
webapp@0.0.1 /Users/tstone/Development/portal/src/main/webapp
└─┬ @angular-devkit/build-angular@0.7.0
  └─┬ webpack-dev-server@3.11.3
    └─┬ del@4.1.1
      └─┬ @types/glob@7.2.0
        └── @types/minimatch@5.1.2

I determined then that I could try upgrading @angular-devkit/build-angular to get new type definitions.然后我决定我可以尝试升级@angular-devkit/build-angular以获得新的类型定义。 I incremented @angular-devkit/build-angular by minor and patch to the first version that compiled cleanly (I stopped on 0.8.9).@angular-devkit/build-angular增加了 minor 并修补到第一个编译干净的版本(我在 0.8.9 上停止了)。

HTH. HTH。 It's a very frustrating error to encounter with legacy applications that won't be upgraded.遇到不会升级的遗留应用程序是一个非常令人沮丧的错误。 Especially, when "old hands" working with the code say it "works on their machine," but admit they haven't tried building it in years and the CI hasn't run recently either.特别是,当使用代码的“老手”说它“在他们的机器上工作”时,但承认他们多年来没有尝试构建它并且 CI 最近也没有运行。

Check if you are using the latest version of typescript, if not then try https://stackoverflow.com/a/46399668/1817690 . 检查您是否正在使用最新版本的打字稿,如果没有,请尝试https://stackoverflow.com/a/46399668/1817690

Also, of in older versions of rxjs will not work, so change it in your service file 此外, of旧版本中rxjs将无法正常工作,那么改变它在你的服务文件

import { Observable, Subject } from 'rxjs';

and use 和使用

import "rxjs/add/observable/of";

or 要么

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

Read the answers given for import .of() for Observable in typescript 阅读打字稿中可观察的import .of()给出的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM