我有這個界面:
export interface AuthStateInterface {
isSubmitting: boolean,
X_HI_CODE: string,
token: string,
userId : number,
roleId: number
}
我想創建一個這種介面型別的物件,其中一些資料是從另一個類中的 ngrx 存盤中收集的,使用方法 initializeValues() 創建物件如下:
initializeValues(): void {
const data: AuthStateInterface = {
isSubmitting: false,
X_HI_CODE: this.store.pipe(select(xhicodeSelector)),
token: this.store.pipe(select(tokenSelector)),
userId : this.store.pipe(select(userIdSelector)),
roleId: this.store.pipe(select( roleIdSelector)),
};
console.log('data', data)
}
但是如您在下面的螢屏截圖中所見,Visual Studio 會出錯:
當我將滑鼠懸停在紅色警報上時,它是這樣說的:
如果有人能幫我解決這個問題,我會很高興的。
uj5u.com熱心網友回復:
一般來說,我誤解了幾個角度或打字稿的概念。正如@MikeOne 在評論中所說,Ngrx 選擇器回傳 Observables,我必須訂閱并將它們分配給我之前宣告的變數。所以這就是我所做的:
1-我初始化了變數,而不是可觀察的:
isSubmitting!: boolean
token!: string
roleId!: [number]
userId!: string
2-我使用選擇器鉤子從ngrx-store中選擇了資料
ngOnInit(): void {
this.initializeValues()
this.getAllStaff()
}
initializeValues(): void {
this.store
.pipe(select(isSubmittingSelector), map((isSubmitting) => isSubmitting))
.subscribe(value => this.isSubmitting = value);
this.store
.pipe(select(tokenSelector), map((token) => token))
.subscribe(value => this.token = value);
this.store
.pipe(select(roleIdSelector), map((roleId) => roleId))
.subscribe(value => this.roleId = value);
this.store
.pipe(select(userIdSelector), map((userId) => userId))
.subscribe(value => this.userId = value);
}
3- 然后我使用這些值創建傳遞給下一個承諾的資料
getAllPersnlValues() {
const data: AuthStateInterface = {
isSubmitting: this.isSubmitting,
X_HI_CODE: this.HI_CODE,
token: this.token,
userId : this.userId,
roleIds: this.roleId,
};
console.log('data', data);
return this.persnlService.getPersnl(data)
.pipe(
map((response) => response)
);
}
應該有更短的方法來做到這一點,并且以我的 Reactjs 背景,在吹噓更簡單的技術之前,我有更多關于 rxjs 的知識。但是這種方法使這項任務完成了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/479280.html