Angular & Firebase.Firestore: 用户的私有和公共集合
创始人
2024-10-14 11:31:51
0

这是一个使用Angular和Firebase.Firestore的解决方法,用于创建用户的私有和公共集合。

首先,确保您已经安装了Angular和Firebase,并在Firebase控制台中创建了您的项目。

  1. 配置Firebase 在src/environments目录下创建两个环境文件:environment.ts和environment.prod.ts,并添加以下代码:
// environment.ts
export const environment = {
  production: false,
  firebase: {
    apiKey: "YOUR_API_KEY",
    authDomain: "YOUR_AUTH_DOMAIN",
    projectId: "YOUR_PROJECT_ID",
    storageBucket: "YOUR_STORAGE_BUCKET",
    messagingSenderId: "YOUR_MESSAGING_SENDER_ID",
    appId: "YOUR_APP_ID",
    measurementId: "YOUR_MEASUREMENT_ID"
  }
};

// environment.prod.ts
export const environment = {
  production: true,
  firebase: {
    apiKey: "YOUR_API_KEY",
    authDomain: "YOUR_AUTH_DOMAIN",
    projectId: "YOUR_PROJECT_ID",
    storageBucket: "YOUR_STORAGE_BUCKET",
    messagingSenderId: "YOUR_MESSAGING_SENDER_ID",
    appId: "YOUR_APP_ID",
    measurementId: "YOUR_MEASUREMENT_ID"
  }
};

将YOUR_API_KEY等替换为您在Firebase控制台中找到的实际值。

  1. 配置Angular应用 在src/app目录下创建一个名为firebase.service.ts的新文件,并添加以下代码:
import { Injectable } from '@angular/core';
import { AngularFireAuth } from '@angular/fire/auth';
import { AngularFirestore, AngularFirestoreCollection, DocumentReference } from '@angular/fire/firestore';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

export interface User {
  uid: string;
  email: string;
}

export interface PrivateData {
  uid: string;
  privateField: string;
}

export interface PublicData {
  uid: string;
  publicField: string;
}

@Injectable({
  providedIn: 'root'
})
export class FirebaseService {
  private usersCollection: AngularFirestoreCollection;
  private privateDataCollection: AngularFirestoreCollection;
  private publicDataCollection: AngularFirestoreCollection;

  constructor(
    private afAuth: AngularFireAuth,
    private afs: AngularFirestore
  ) {
    this.usersCollection = this.afs.collection('users');
    this.privateDataCollection = this.afs.collection('privateData');
    this.publicDataCollection = this.afs.collection('publicData');
  }

  createUser(user: User): Promise> {
    return this.usersCollection.add(user);
  }

  createPrivateData(privateData: PrivateData): Promise> {
    return this.privateDataCollection.add(privateData);
  }

  createPublicData(publicData: PublicData): Promise> {
    return this.publicDataCollection.add(publicData);
  }

  getUser(uid: string): Observable {
    return this.usersCollection.doc(uid).valueChanges();
  }

  getPrivateData(uid: string): Observable {
    return this.privateDataCollection
      .valueChanges({ idField: 'id' })
      .pipe(
        map((data) => data.filter((d) => d.uid === uid))
      );
  }

  getPublicData(): Observable {
    return this.publicDataCollection.valueChanges({ idField: 'id' });
  }
}
  1. 创建组件 在您的Angular应用中创建一个组件(例如user.component.ts)来展示用户的私有和公共数据,并在该组件中使用FirebaseService。
import { Component, OnInit } from '@angular/core';
import { FirebaseService, PublicData, PrivateData, User } from 'src/app/firebase.service';
import { Observable } from 'rxjs';

@Component({
  selector: 'app-user',
  templateUrl: './user.component.html',
  styleUrls: ['./user.component.css']
})
export class UserComponent implements OnInit {
  user: User;
  privateData: Observable;
  publicData: Observable;

  constructor(private firebaseService: FirebaseService) { }

  ngOnInit(): void {
    // 获取当前用户
    this.firebaseService.getUser('CURRENT_USER_UID').subscribe((user) => {
      this.user = user;
    });

    // 获取当前用户的私有数据
    this.privateData = this.firebaseService.getPrivateData('CURRENT_USER_UID');

    // 获取所有公共数据
    this.publicData = this.firebaseService.getPublicData();
  }

  createPrivateData(): void {
    const privateData: PrivateData =

相关内容

热门资讯

Android Recycle... 要在Android RecyclerView中实现滑动卡片效果,可以按照以下步骤进行操作:首先,在项...
安装apache-beam==... 出现此错误可能是因为用户的Python版本太低,而apache-beam==2.34.0需要更高的P...
Android - 无法确定任... 这个错误通常发生在Android项目中,表示编译Debug版本的Java代码时出现了依赖关系问题。下...
Android - NDK 预... 在Android NDK的构建过程中,LOCAL_SRC_FILES只能包含一个项目。如果需要在ND...
Alertmanager在pr... 首先,在Prometheus配置文件中,确保Alertmanager URL已正确配置。例如:ale...
Akka生成Actor问题 在Akka框架中,可以使用ActorSystem对象生成Actor。但是,当我们在Actor类中尝试...
Agora-RTC-React... 出现这个错误原因是因为在 React 组件中使用,import AgoraRTC from “ago...
Aksnginxdomainb... 在AKS集群中,可以使用Nginx代理服务器实现根据域名进行路由。以下是具体步骤:部署Nginx i...
Alertmanager中的基... Alertmanager中可以使用repeat_interval选项指定在一个告警重复发送前必须等待...
AddSingleton在.N... 在C#中创建Singleton对象通常是通过私有构造函数和静态属性来实现,例如:public cla...