Angular 具有动态参数的角度工厂提供程序
对于动态计算的firebase firestore路径,我有以下提供程序工厂:Angular 具有动态参数的角度工厂提供程序,angular,typescript,firebase,dependency-injection,angular7,Angular,Typescript,Firebase,Dependency Injection,Angular7,对于动态计算的firebase firestore路径,我有以下提供程序工厂: const meetingServiceFactory = (db: AngularFirestore, authService: AuthService ) => { const userId = authService.user; return new MeetingService('/users/' + userId + '/meetings', db); }; export let mee
const meetingServiceFactory = (db: AngularFirestore, authService: AuthService ) => {
const userId = authService.user;
return new MeetingService('/users/' + userId + '/meetings', db);
};
export let meetingServiceProvider = { provide: MeetingService,
useFactory: meetingServiceFactory,
deps: [AngularFirestore, AuthService] };
这里是会议服务:
@Injectable(
{
providedIn: 'root'
}
)
export class MeetingService extends FirestoreDataService<Meeting> {
constructor(path: string, db: AngularFirestore) {
super(path, db);
}
setProposalFlag(meetingKey: string) {
return from(this.db.doc(this.path + `/${meetingKey}`).update({hasProposal: true})
.then(result => console.log('changed active Flag on meeting: ', result))
.catch(err => console.log(err)));
}
saveMeeting(meeting: Meeting) {
return of(meeting).pipe(
map(meetingToSave => {
return {...meetingToSave, key: this.db.createId() };
}),
switchMap( finalMeeting => this.addItemWithKey(finalMeeting.key, finalMeeting) )
);
}
getAll() {
return this.getItems();
}
}
@可注入(
{
providedIn:'根'
}
)
导出类会议服务扩展FirestoreDataService{
构造函数(路径:字符串,数据库:AngularFirestore){
超级(路径,db);
}
setProposalFlag(meetingKey:string){
从(this.db.doc(this.path+`/${meetingKey}}`)返回.update({hasproval:true})
.then(result=>console.log('changed active Flag on meeting:',result'))
.catch(err=>console.log(err));
}
保存会议(会议:会议){
返回(会议)。管道(
地图(meetingToSave=>{
返回{…meetingToSave,key:this.db.createId()};
}),
开关映射(finalMeeting=>this.addItemWithKey(finalMeeting.key,finalMeeting))
);
}
getAll(){
返回这个.getItems();
}
}
这继承自FirestoreDataService
,因此我有一种CRUD服务可重用:
@Injectable()
export class FirestoreDataService<T> {
protected itemCollection: AngularFirestoreCollection<T>;
protected items$: Observable<T[]>;
protected snapshot$: Observable<DocumentChangeAction<T>[]>;
protected state$: Observable<DocumentChangeAction<T>[]>;
constructor(protected path: string, protected db: AngularFirestore) {
this.itemCollection = this.db.collection<T>(path);
this.items$ = this.itemCollection.valueChanges();
this.snapshot$ = this.itemCollection.snapshotChanges();
this.state$ = this.itemCollection.stateChanges();
}
getItems(): Observable<T[]> {
return this.items$;
}
getItemWithKey( key: string): Observable<T> {
// used to use stateChanges here.
// as the doc states, this only emits recent changes,
// so if we subscribe in an async pipe, it will not reemit and subsequent observables will be empty.
return this.itemCollection.doc<T>(key).valueChanges();
}
addItem(data: T) {
return from(this.itemCollection.add(data)
.catch(err => console.log('Error while adding item: ', err)));
}
addItemWithKey(key: string, data: T) {
return from(this.itemCollection.doc(key).set(data)
.catch(err => console.log('Error while adding item: ', err)));
}
deleteItem(key: string) {
return from(this.itemCollection.doc(key).delete()
.catch(err => console.log('Error while deleting item: ', err)));
}
}
@Injectable()
导出类FirestoreDataService{
受保护项目集合:AngularFirestoreCollection;
受保护物品$:可观察;
受保护快照$:可观察;
受保护状态$:可观察;
构造函数(受保护路径:字符串,受保护数据库:AngularFirestore){
this.itemCollection=this.db.collection(路径);
this.items$=this.itemCollection.valueChanges();
this.snapshot$=this.itemCollection.snapshotChanges();
this.state$=this.itemCollection.stateChanges();
}
getItems():可观察{
返回此项。项$;
}
getItemWithKey(键:字符串):可观察{
//用于在此处使用状态更改。
//如文档所述,这只会发出最近的更改,
//因此,如果我们在一个异步管道中订阅,它将不会重新提交,后续的观测值将为空。
返回此.itemCollection.doc(key).valueChanges();
}
附加项(数据:T){
从(this.itemCollection.add)返回(数据)
.catch(err=>console.log('添加项时出错:',err));
}
addItemWithKey(键:字符串,数据:T){
从(this.itemCollection.doc(key).set(data)返回
.catch(err=>console.log('添加项时出错:',err));
}
deleteItem(键:字符串){
从(this.itemCollection.doc(key).delete()返回
.catch(err=>console.log('删除项时出错:',err));
}
}
,但这显然取决于用户ID的运行时决策。在ng serve中似乎工作正常
但是在为prod传输时,我得到:错误:无法解析/web client/src/app/core/services/firestore-data.service.ts:(?,[object object])中FirestoreDataService的所有参数。
我假设这是因为我不知道编译时的确切路径。是这样吗?如果没有,此设置会有什么问题?我将如何修复/改进它?我现在跑了7英里。谢谢你的帮助 我通过重构解决了这个问题。希望这对某些人有用
@Injectable(
{
providedIn: 'root'
}
)
export class MeetingService extends FirestoreDataService<Meeting> {
constructor(authService: AuthService,
db: AngularFirestore) {
const path = `/users/${authService.user}/meetings`;
super(path, db);
this.auth = authService;
}
...
}
@可注入(
{
providedIn:'根'
}
)
导出类会议服务扩展FirestoreDataService{
构造函数(authService:authService,
db:AngularFirestore(零售店){
const path=`/users/${authService.user}/meetings`;
超级(路径,db);
this.auth=authService;
}
...
}
请注意,这只是因为以这种方式注入服务,参数对构造函数是私有的,否则,第一个调用必须是
super(path,db)
您是否尝试从FirestoreDataService
中删除@Injectable()
装饰器?因为您自己正在实例化类实例,没有角度DI。@BorysKupar。是的,我试过了。不起作用。是否与该案例中显示的错误相同?如何使用meetingServiceProvider
?@BorysKupar是的,相同的错误。我将其插入我的模块:providers:[FormBuilder、MeetingForm、meetingServiceProvider、GeocodingService]