return Observable<boolean> Angular 面临问题
Facing problem with return Observable<boolean> Angular
我遇到了 angular 的问题。我创建了一个管道,该管道应该调用另一个管道并使用循环检查数组的所有元素是否为假。如果有一个为真ill return true.. 我不知道为什么我不允许 return 一个 Observable 布尔值..
我整天都在为这个错误而苦苦挣扎。如果有人能帮助我,我将不胜感激
错误:
TS2322: Type 'Observable' is not assignable to type
'Observable'. Type 'void' is not assignable to type
'boolean'.
transform(antwortElem: UiKat[]): Observable<boolean> {
if(!antwortElem){
return null;
}
return this.service.enabledState$.pipe(
map(enabledState => {
let enabled = false;
Elem.forEach((antwort) => {
if(enabledState[antwort.id] != null){
return enabledState[antwort.id];
}else{
return true;
}
})
})
)
}
map
需要 return 一些东西。在你的情况下你做了一个 forEach
应该是 returned
for(let antwort in antwortElem) {
if(enabledState[antwort.struktur.id] != null){
return enabledState[antwort.struktur.id];
}else{
return true;
}
})
错误表明您 return 什么都没有,这就是为什么它是 void
transform(antwortElem: UiKategorie[]): Observable<boolean> {
if(!antwortElem){
return null; // this probably isn't allowed for your typings. needs to be Observable<boolean> | null
}
return this.iqsService.enabledStateInbearbeitung$.pipe(
map(enabledState => {
// for each doesn't work that way, use the some operator instead
// some will return true if any element matches the condition
return antwortElem.some(antwort => !!enabledState[antwort.struktur.id])
})
)
}
我遇到了 angular 的问题。我创建了一个管道,该管道应该调用另一个管道并使用循环检查数组的所有元素是否为假。如果有一个为真ill return true.. 我不知道为什么我不允许 return 一个 Observable 布尔值..
我整天都在为这个错误而苦苦挣扎。如果有人能帮助我,我将不胜感激
错误:
TS2322: Type 'Observable' is not assignable to type 'Observable'. Type 'void' is not assignable to type 'boolean'.
transform(antwortElem: UiKat[]): Observable<boolean> {
if(!antwortElem){
return null;
}
return this.service.enabledState$.pipe(
map(enabledState => {
let enabled = false;
Elem.forEach((antwort) => {
if(enabledState[antwort.id] != null){
return enabledState[antwort.id];
}else{
return true;
}
})
})
)
}
map
需要 return 一些东西。在你的情况下你做了一个 forEach
应该是 returned
for(let antwort in antwortElem) {
if(enabledState[antwort.struktur.id] != null){
return enabledState[antwort.struktur.id];
}else{
return true;
}
})
错误表明您 return 什么都没有,这就是为什么它是 void
transform(antwortElem: UiKategorie[]): Observable<boolean> {
if(!antwortElem){
return null; // this probably isn't allowed for your typings. needs to be Observable<boolean> | null
}
return this.iqsService.enabledStateInbearbeitung$.pipe(
map(enabledState => {
// for each doesn't work that way, use the some operator instead
// some will return true if any element matches the condition
return antwortElem.some(antwort => !!enabledState[antwort.struktur.id])
})
)
}