[英]Type 'boolean' is not assignable to type 'ObservableInput<{}>'
I am working on angular 6 project. 我正在从事angular 6项目。 I am using canDeactivate for my routeGuards and a popup to show route leave message.
我正在为我的routeGuards使用canDeactivate和一个弹出以显示路线离开消息。 But the issue is coming at my price-list-guard-service on hover .flatMap(isAllow)=> {
但是问题出在悬停.flatMap(isAllow)=> {
Error: Argument of type '(isAllow: boolean) => boolean' is not assignable to parameter of type '(value: boolean, index: number) => ObservableInput<{}>'.. 错误: 类型'(isAllow:boolean)=>布尔值'的参数不能分配给类型'(值:布尔值,索引:数字)=> ObservableInput <{}>'的参数。
I wanted to do something like this in price-list-guard.service.ts: 我想在price-list-guard.service.ts中执行以下操作:
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;
}
});
}
}
}
prompt-service.ts
提示-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);
}
}
Your canDeactivate
method return type is Boolean. 您的
canDeactivate
方法返回类型为Boolean。 but the method looks like nothing is return. 但是该方法看起来没有任何回报。 So try this below method instead of your method
因此,请尝试以下方法代替您的方法
canDeactivate(component: PriceListFormComponent):boolean {
// let retVal: boolean = true;
if (component.isDirty) {
this.promptService.showPrompt('Warning', 'Unsaved changes detectect on the current page);
return this.promptService.callback.flatMap((isAllow) => {
if (isAllow) {
return true;
} else {
return false;
}
});
}
return false;
}
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.