sidebar.service.ts(仪表板组件的服务文件):
constructor(private db: AngularFireDatabase) {}
getUsers(): Observable<any[]> {
return this.db.list('/users').valueChanges();
}
仪表板.组件.ts:
import { SidebarService } from './sidebar.service';
ngOnInit() {
this.fetchUsers();
}
fetchUsers(): void {
this.sidebarService.getUsers().subscribe(users => {
this.users = users;
this.filteredUsers = this.users;
}, error => {
console.error('Error fetching users:', error);
});
开发工具错误:
ERROR NullInjectorError: NullInjectorError: No provider for InjectionToken angularfire2.app.options!
所以每次我登录/注册时,页面都会变成白色,开发工具会出现错误,并且不会带我进入仪表板组件(链接保持http://localhost:4200/login或http://localhost:4200/注册)
尝试更改访问数据库的方式。
import { Firestore, collectionData, collection } from '@angular/fire/firestore';
@Injectable({
providedIn: 'root'
})
export class SidebarService {
private firestore: Firestore = inject(Firestore); // <- changed here!
constructor() {
}
getUsers(): Observable<any[]> {
const itemCollection = collection(this.firestore, 'users');
return collectionData<Item>(itemCollection);
}
}