我正在处理 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<{}>"
发布评论