无法使用从 ngrx 存储收集的数据创建接口类型的对象
Can't create an object of type interface with data collected from ngrx store
我有这个界面:
export interface AuthStateInterface {
isSubmitting: boolean,
X_HI_CODE: string,
token: string,
userId : number,
roleId: number
}
并且我想使用从另一个 class 中的 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 出错了,您可以在下面的屏幕截图中看到:
当我将鼠标悬停在红色警报上时,它是这样说的:
如果有人能帮我解决这个问题,我将不胜感激。
我对 angular 或打字稿的几个概念有误解。正如@MikeOne 在评论中所说,Ngrx 选择器 return 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 的知识。但是这种做法让这个任务完成了。
我有这个界面:
export interface AuthStateInterface {
isSubmitting: boolean,
X_HI_CODE: string,
token: string,
userId : number,
roleId: number
}
并且我想使用从另一个 class 中的 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 出错了,您可以在下面的屏幕截图中看到:
当我将鼠标悬停在红色警报上时,它是这样说的:
如果有人能帮我解决这个问题,我将不胜感激。
我对 angular 或打字稿的几个概念有误解。正如@MikeOne 在评论中所说,Ngrx 选择器 return 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 的知识。但是这种做法让这个任务完成了。