类型“boolean”不可分配给类型“ObservableInput<{}>”

2023-12-31

我正在开发 Angular 6 项目。我正在为我的routeGuards 使用 canDeactivate 并使用弹出窗口来显示路线留下消息。但问题出在我的价格表守卫服务上悬停 .flatMap(isAllow)=> {

Error: 类型“(isAllow: boolean) => boolean”的参数不可分配给类型“(value: boolean, index: number) => ObservableInput”的参数。

我想在 Price-list-guard.service.ts 中做这样的事情:

价格表-guard.service.ts

@Injectable()
export class PriceListFormGuard implements CanDeactivate<PriceListFormComponent> {
    constructor(private promptService: PromptService) { }

    canDeactivate(component: PriceListFormComponent):boolean {
        if (component.isDirty) {
            this.promptService.showPrompt('Warning', 'Unsaved changes detectect on the current page);
            this.promptService.callback.flatMap((isAllow) => {
                if (isAllow) {
                    return true;
                } else {
                    return false;
                }
            });
        }
    }
}

提示服务.ts

@Injectable()
export class PromptService {
    title: string;
    message: string;
    display = 'none';
    callback: Subject<boolean>;

    constructor() {
        this.callback = new Subject<boolean>();
    }

    showPrompt(title: string, message: string): void {
        this.title = title;
        this.message = message;
        this.display = 'block';
    }

    close(confirm?: boolean): void {
        this.title = null;
        this.message = null;
        this.display = 'none';
        if (confirm != null) {
            this.callback.next(confirm);
        }
    }

使用异步操作时无法返回布尔值。

改成这样:

canDeactivate(component: PriceListFormComponent):Observable<boolean> { // Return an observable
    if (component.isDirty) {
        this.promptService.showPrompt('Warning', 'Unsaved changes detectect on the 
        current page);
        return this.promptService.callback.asObservable();
    } else {
        return of(true);
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

类型“boolean”不可分配给类型“ObservableInput<{}>” 的相关文章

随机推荐