简体   繁体   English

使用 firebase.User 方法从 firebase angular 项目获取登录用户的用户名时出错

[英]Getting error while using firebase.User method to get username of logged in user from firebase angular project

Below are my lines of code, I have an error getting the current username to display on the browser.下面是我的代码行,我在获取当前用户名以显示在浏览器上时出错。

I created a Navbar that has a dropdown list only to be shown to the logged-in user.我创建了一个 Navbar,它有一个下拉列表,只显示给登录用户。 Login also works perfectly but I cannot get the current user name to show in the drop-down menu.登录也能正常工作,但我无法在下拉菜单中显示当前用户名。 I am using google authentication using firebase.我正在使用 firebase 使用谷歌身份验证。

I am getting below error on below line,我在下面的行中遇到错误,

user: firebase.User;

Error:错误:

Namespace '"E:/Workspace/Angular/oshop/node_modules/firebase/app/dist/app/index"' has no exported member 'User'".命名空间“E:/Workspace/Angular/oshop/node_modules/firebase/app/dist/app/index”没有导出成员“User”。

See my code below.请参阅下面的代码。

bs-navbar.component.ts bs-navbar.component.ts

import { AngularFireAuth } from '@angular/fire/compat/auth';
import { AngularFireModule } from '@angular/fire/compat';
import { Component, OnInit } from '@angular/core';
import * as firebase from 'firebase/app';
import { getAuth, onAuthStateChanged } from "firebase/auth";

//const auth = getAuth();
//const user = auth.currentUser;

@Component({
  selector: 'bs-navbar',
  templateUrl: './bs-navbar.component.html',
  styleUrls: ['./bs-navbar.component.css']
})
export class BsNavbarComponent {  
  user: firebase.User;

  constructor(private afAuth: AngularFireAuth) {
    afAuth.authState.subscribe(user => this.user = user)
   }

  logout(){
    this.afAuth.signOut().then(() => {
      window.alert('Logged out');
    })
  }

}

You can import User from Firebase Auth SDK as shown below:您可以从 Firebase 导入User Auth SDK 如下所示:

// Remove this import
// import * as firebase from 'firebase/app';

import { User, getAuth, onAuthStateChanged } from "firebase/auth";

export class BsNavbarComponent {  
  user: User;
}

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

相关问题 firebase.firebase 版本 9 中的用户 - firebase.User in firebase version 9 如何使用 Firebase 从云 function 端查找用户是否已登录? - How to find user is logged in or not from cloud function side using Firebase? 访问 firebase 用户的 photoURL 时出现 http 403 错误 - Getting http 403 error while accessing photoURL of a firebase User 在反应中使用 firebase 检查用户是否登录 - Check if user is logged in or not using firebase in react 在页面加载之前获取登录用户 Javascript Firebase - Get logged in user before page load Javascript Firebase 我无法从特定用户获取 firebase 的数据(使用 uid 检查方法) - I can not get data from firebase from a specific user (using the uid check method) 如何使用 Firebase 身份验证保持用户登录 Flutter web 应用程序 - How to keep a user logged in Flutter web app using Firebase Authentication 如何检索当前从 Firestore/Firebase 登录的用户信息? - How to retrieve the user's info that is currently logged in from Firestore/Firebase? Flutter Firebase:如何通过用户名获取用户ID? - Flutter Firebase : How to get a user's id by his username? 使用 firebase 作为身份验证器登录时,用户会立即注销 - User is instantly logged out when logging in using firebase as authenticator on react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM