类型“boolean"不可分配给类型“ObservableInput<{}>" [英] Type &#39;boolean&#39; is not assignable to type &#39;ObservableInput&lt;{}&gt;&#39;

查看:78
本文介绍了类型“boolean"不可分配给类型“ObservableInput<{}>"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理 angular 6 项目.我正在使用 canDeactivate 作为我的 routeGuards 和一个弹出窗口来显示路由离开消息.但问题出现在我的价目表保护服务上 .flatMap(isAllow)=> {

错误:'(isAllow: boolean) => boolean' 类型的参数不可分配给类型'(value: boolean, index: number) => ObservableInput<{}>'..

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

<块引用>

price-list-guard.service.ts

@Injectable()导出类 PriceListFormGuard 实现了 CanDeactivate{构造函数(私有提示服务:提示服务){}canDeactivate(component: PriceListFormComponent):boolean {如果(组件.isDirty){this.promptService.showPrompt('Warning', '当前页面上未保存的更改检测);this.promptService.callback.flatMap((isAllow) => {如果(是允许){返回真;} 别的 {返回假;}});}}}

<块引用>

提示服务.ts

@Injectable()导出类 PromptService {标题:字符串;消息:字符串;显示 = '无';回调:主题<布尔值>;构造函数(){this.callback = new Subject();}showPrompt(title: string, message: string): void {this.title = 标题;this.message = 消息;this.display = 'block';}关闭(确认?:布尔值):无效{this.title = null;this.message = null;this.display = '无';如果(确认!= null){this.callback.next(确认);}}

解决方案

使用异步操作时不能返回布尔值.

改成这样:

canDeactivate(component: PriceListFormComponent):Observable{//返回一个可观察对象如果(组件.isDirty){this.promptService.showPrompt('Warning', '未保存的更改检测当前页面);返回 this.promptService.callback.asObservable();} 别的 {返回(真);}}

I am working on angular 6 project. I am using canDeactivate for my routeGuards and a popup to show route leave message. But the issue is coming at my price-list-guard-service on hover .flatMap(isAllow)=> {

Error: Argument of type '(isAllow: boolean) => boolean' is not assignable to parameter of type '(value: boolean, index: number) => ObservableInput<{}>'..

I wanted to do something like this in price-list-guard.service.ts:

price-list-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;
                }
            });
        }
    }
}

prompt-service.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);
        }
    }

解决方案

You can't return a boolean when using async operations.

Change to this:

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);
    }
}

这篇关于类型“boolean"不可分配给类型“ObservableInput<{}>"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆