Saya sedang mengerjakan proyek Angular dan saya menemukan masalah berikut di kelas ini:

import { Component, OnInit } from '@angular/core';
import {SidebarModule} from 'primeng/sidebar';
import { AngularFireAuth } from '@angular/fire/auth';
import { Observable } from 'rxjs';

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

  display;

  isLoggedIn$: Observable<boolean>;
  isLoggedOut$:Observable<boolean>;
  pictureUrl$: Observable<string>;


  constructor(private afAuth: AngularFireAuth) { }

  ngOnInit(): void {
    this.afAuth.authState.subscribe(user => console.log(user));

    this.isLoggedIn$ = this.afAuth.authState.pipe(map(user => !!user));

    this.isLoggedOut$ = this.isLoggedIn$.pipe(map(loggedIn => !loggedIn));

    this.pictureUrl$ =
      this.afAuth.authState.pipe(map(user => user ? user.photoURL: null));
  }


  logout() {
    this.afAuth.auth.signOut();
  }

}

Masalah muncul pada panggilan map() dari 3 baris berikut:

this.isLoggedIn$ = this.afAuth.authState.pipe(map(user => !!user));

this.isLoggedOut$ = this.isLoggedIn$.pipe(map(loggedIn => !loggedIn));

this.pictureUrl$ =
  this.afAuth.authState.pipe(map(user => user ? user.photoURL: null));

IDE memberi saya pesan kesalahan ini:

Cannot find name 'map'. Did you mean 'Map'?ts(2552)
lib.es2015.collection.d.ts(36, 13): 'Map' is declared here.

Mencoba mengkompilasi saya mendapatkan pesan kesalahan ini:

ERROR in src/app/header/header.component.ts:25:51 - error TS2552: Cannot find name 'map'. Did you mean 'Map'?

25     this.isLoggedIn$ = this.afAuth.authState.pipe(map(user => !!user));
                                                     ~~~

  node_modules/typescript/lib/lib.es2015.collection.d.ts:36:13
    36 declare var Map: MapConstructor;
                   ~~~
    'Map' is declared here.
src/app/header/header.component.ts:27:47 - error TS2552: Cannot find name 'map'. Did you mean 'Map'?

27     this.isLoggedOut$ = this.isLoggedIn$.pipe(map(loggedIn => !loggedIn));
                                                 ~~~

  node_modules/typescript/lib/lib.es2015.collection.d.ts:36:13
    36 declare var Map: MapConstructor;
                   ~~~
    'Map' is declared here.
src/app/header/header.component.ts:30:34 - error TS2552: Cannot find name 'map'. Did you mean 'Map'?

30       this.afAuth.authState.pipe(map(user => user ? user.photoURL: null));
                                    ~~~

  node_modules/typescript/lib/lib.es2015.collection.d.ts:36:13
    36 declare var Map: MapConstructor;
                   ~~~
    'Map' is declared here.

Mengapa? Apa yang salah? Apa yang saya lewatkan? Bagaimana saya bisa mencoba memperbaiki kode ini?

-2
AndreaNobili 9 Juli 2020, 14:10

1 menjawab

Jawaban Terbaik

Anda perlu mengimpor operator RxJS map: import {map} from 'rxjs/operators';

2
vadimk7 9 Juli 2020, 11:12