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

编程入门 行业动态 更新时间:2024-10-28 03:24:41
本文介绍了类型“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<{}>"

本文发布于:2023-11-14 02:31:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585989.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   不可分   boolean   gt   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!