这是一个使用Angular和Firebase.Firestore的解决方法,用于创建用户的私有和公共集合。
首先,确保您已经安装了Angular和Firebase,并在Firebase控制台中创建了您的项目。
// 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控制台中找到的实际值。
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' });
}
}
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 =