Angular 8应用程序编译显示:“错误TS1005:':'预期”

abin shaju

嗨,我正在通过观看教程来开发Angle 8应用程序,在标头组件中,我想要当前登录用户的电子邮件ID。我的auth.service.ts如下所示:

import { Injectable } from "@angular/core";

import { AngularFireAuth } from "@angular/fire/auth";

@Injectable({
  providedIn: "root",
})
export class AuthService {
  constructor(private auth: AngularFireAuth) {}

  signUp(email: string, password: string) {
    return this.auth.auth.createUserWithEmailAndPassword(email, password);
  }

  signIn(email: string, password: string) {
    return this.auth.auth.signInWithEmailAndPassword(email, password);
  }

  getUser() {
    return this.auth.authState;
  }
  signOut() {
    return this.auth.auth.signOut();
  }
}

我的header.component.ts是:

import { Component, OnInit } from "@angular/core";
import { AuthService } from "src/app/services/auth.service";
import { ToastrService } from "ngx-toastr";
import { Router } from "@angular/router";

@Component({
  selector: "app-header",
  templateUrl: "./header.component.html",
  styleUrls: ["./header.component.css"],
})
export class HeaderComponent implements OnInit {
  email:string = null;
  constructor(
    private auth: AuthService,
    private router: Router,
    private toastr: ToastrService
  ) {
    auth.getUser().subscribe((user)=>{
      console.log("User is:",user);
      this.email = user?.email;
    })
  }

  ngOnInit() {}

  async handSignOut(){
    try {
      await this.auth.signOut();
      this.router.navigateByUrl("/signin");
      this.toastr.info("Logout sucess");
      this.email = null;
    } catch (error) {
      this.toastr.error("Problem in Signout");
      
    }
  }
}

当我编译代码时,它显示:

ERROR in src/app/layout/header/header.component.ts:20:25 - error TS1109: Expression expected.

20       this.email = user?.email;
                           ~
src/app/layout/header/header.component.ts:20:31 - error TS1005: ':' expected.

20       this.email = user?.email;
                                 ~

当我删除“?” 内联:this.email = user?.email; 它编译成功。我该如何解决这个问题?

我的package.json:

{
  "name": "travelgram",
  "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": "~8.2.7",
    "@angular/common": "~8.2.7",
    "@angular/compiler": "~8.2.7",
    "@angular/core": "~8.2.7",
    "@angular/fire": "^5.4.2",
    "@angular/forms": "~8.2.7",
    "@angular/platform-browser": "~8.2.7",
    "@angular/platform-browser-dynamic": "~8.2.7",
    "@angular/router": "~8.2.7",
    "@fortawesome/angular-fontawesome": "^0.6.1",
    "@fortawesome/fontawesome-svg-core": "^1.2.29",
    "bootstrap": "^4.5.0",
    "browser-image-resizer": "^2.1.0",
    "firebase": "^7.15.4",
    "ngx-toastr": "^10.1.0",
    "rxjs": "~6.4.0",
    "tslib": "^1.10.0",
    "uuid": "^8.2.0",
    "zone.js": "~0.9.1"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.803.5",
    "@angular/cli": "~8.3.5",
    "@angular/compiler-cli": "~8.2.7",
    "@angular/language-service": "~8.2.7",
    "@types/node": "~8.9.4",
    "@types/jasmine": "~3.3.8",
    "@types/jasminewd2": "~2.0.3",
    "codelyzer": "^5.0.0",
    "jasmine-core": "~3.4.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.1.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~2.0.1",
    "karma-jasmine-html-reporter": "^1.4.0",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.15.0",
    "typescript": "~3.5.3",
    "@angular-devkit/architect": "<0.900 || ^0.900.0-0 || ^9.0.0-0",
    "firebase-tools": "^7.12.0",
    "fuzzy": "^0.1.3",
    "inquirer": "^6.2.2",
    "inquirer-autocomplete-prompt": "^1.0.1"
  }
}
瓦西姆

尝试这个

this.email = user ? user.email ? user.email :"Email Null" : "User Null";

要么

if(user != null && user!= undefined)
{
  if(user.email != null && user.email != undefined)
   {
    this.email = user.email;
   }
}

要么

if( (user != null && user!= undefined) && (user.email != null && user.email != undefined))
{
    this.email = user.email;  
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

错误 TS1005: ',' 预期,未能编译 angular 6 项目

来自分类Dev

Angular 2 错误 TS1005: ',' 预期

来自分类Dev

Angular 8 Firebase获取node_modules / firebase / index.d.ts(4369,38):错误TS1005:';' 预期

来自分类Dev

错误 TS1005: ';' 预期的

来自分类Dev

羽毛笔编辑器在Angular 8应用程序中未按预期显示

来自分类Dev

错误 TS1005: ':' 尝试实例化接口时预期

来自分类Dev

Angular AOT 编译的应用程序不像预期的那样摇树

来自分类Dev

尝试运行 Angular 8 应用程序时出现“未捕获的错误:预期‘样式’为字符串数组”

来自分类Dev

面向TypeScript','预期将Angular2-rc1升级到Angular2-rc2。(TS1005)

来自分类Dev

src/app/logger.ts(18,16) 中的错误:错误 TS1005:'(' 预期

来自分类Dev

src/app/app.component.ts(72,69) 中的错误:错误 TS1005:“,”预期

来自分类Dev

打字稿错误TS1005:预期为“:”。与Object.assign()

来自分类Dev

错误: ';' 预期的

来自分类Dev

获取:错误 TS1128:预期的声明或语句。在 ReactJS 和 TypeScript 应用程序中

来自分类Dev

编译错误:预期:=

来自分类Dev

编译错误:预期:=

来自分类Dev

带有TypeScript的Angular2:@component之后声明预期的编译器错误

来自分类Dev

Angular 不返回预期值

来自分类Dev

数组中的扩展操作出错。TS1005: ',' 预期。打字稿

来自分类Dev

应用程序设置未按预期工作

来自分类Dev

应用程序设置未按预期工作

来自分类Dev

TwitterKit解析错误。预期的')'和预期的类型

来自分类Dev

TwitterKit解析错误。预期的')'和预期的类型

来自分类Dev

Angular2,ASP.NET 5项目设置。di.d.ts上出现“预期类型”错误

来自分类Dev

angular js预期的响应包含一个对象,但出现数组类型错误

来自分类Dev

打字稿使用Angular 2组件引发了声明预期错误

来自分类Dev

angular js预期的响应包含一个对象,但出现了数组类型错误

来自分类Dev

带有 d3 的 Angular:错误:<path> 属性 d:预期数字,“MNaN,25.384615384...”

来自分类Dev

通过参数解析时如何预期错误?- Angular6,茉莉花

Related 相关文章

  1. 1

    错误 TS1005: ',' 预期,未能编译 angular 6 项目

  2. 2

    Angular 2 错误 TS1005: ',' 预期

  3. 3

    Angular 8 Firebase获取node_modules / firebase / index.d.ts(4369,38):错误TS1005:';' 预期

  4. 4

    错误 TS1005: ';' 预期的

  5. 5

    羽毛笔编辑器在Angular 8应用程序中未按预期显示

  6. 6

    错误 TS1005: ':' 尝试实例化接口时预期

  7. 7

    Angular AOT 编译的应用程序不像预期的那样摇树

  8. 8

    尝试运行 Angular 8 应用程序时出现“未捕获的错误:预期‘样式’为字符串数组”

  9. 9

    面向TypeScript','预期将Angular2-rc1升级到Angular2-rc2。(TS1005)

  10. 10

    src/app/logger.ts(18,16) 中的错误:错误 TS1005:'(' 预期

  11. 11

    src/app/app.component.ts(72,69) 中的错误:错误 TS1005:“,”预期

  12. 12

    打字稿错误TS1005:预期为“:”。与Object.assign()

  13. 13

    错误: ';' 预期的

  14. 14

    获取:错误 TS1128:预期的声明或语句。在 ReactJS 和 TypeScript 应用程序中

  15. 15

    编译错误:预期:=

  16. 16

    编译错误:预期:=

  17. 17

    带有TypeScript的Angular2:@component之后声明预期的编译器错误

  18. 18

    Angular 不返回预期值

  19. 19

    数组中的扩展操作出错。TS1005: ',' 预期。打字稿

  20. 20

    应用程序设置未按预期工作

  21. 21

    应用程序设置未按预期工作

  22. 22

    TwitterKit解析错误。预期的')'和预期的类型

  23. 23

    TwitterKit解析错误。预期的')'和预期的类型

  24. 24

    Angular2,ASP.NET 5项目设置。di.d.ts上出现“预期类型”错误

  25. 25

    angular js预期的响应包含一个对象,但出现数组类型错误

  26. 26

    打字稿使用Angular 2组件引发了声明预期错误

  27. 27

    angular js预期的响应包含一个对象,但出现了数组类型错误

  28. 28

    带有 d3 的 Angular:错误:<path> 属性 d:预期数字,“MNaN,25.384615384...”

  29. 29

    通过参数解析时如何预期错误?- Angular6,茉莉花

热门标签

归档