简体   繁体   English

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

[英]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.

相关问题 switchMap类型void不能分配给ObservableInput &lt;{}&gt;类型 - switchMap Type void is not assignable to type ObservableInput<{}> Angular 8 类型“void”不可分配给类型“ObservableInput”<any> '</any> - Angular 8 Type 'void' is not assignable to type 'ObservableInput<any>' 类型 'void' 不可分配给类型 'ObservableInput<any> '</any> - Type 'void' is not assignable to type 'ObservableInput<any>' Angular 7类型“ void”不可分配给类型“ ObservableInput &lt;{}&gt;” - Angular 7 Type 'void' is not assignable to type 'ObservableInput<{}>' 输入'无效| 可观察的<any> ' 不可分配给类型 'ObservableInput<any> '. 类型“void”不可分配给类型“ObservableInput”<any> '</any></any></any> - Type 'void | Observable<any>' is not assignable to type 'ObservableInput<any>'. Type 'void' is not assignable to type 'ObservableInput<any>' SwitchMap:类型“void”不可分配给类型“ObservableInput”<any> ' 对于 http 响应</any> - SwitchMap: Type 'void' is not assignable to type 'ObservableInput<any>' for http response ngrx 效果导致类型“void”不可分配给类型“ObservableInput” - ngrx effects gives Type 'void' is not assignable to type 'ObservableInput' NGRX 效果抛出错误类型 XXXX 不可分配给类型 'ObservableInput<any> '</any> - NGRX Effect throws Error Type XXXX not assignable to type 'ObservableInput<any>' Angular 6 RxJS6类型“ void”不能分配给类型“ ObservableInput &lt;{}&gt;” - Angular 6 RxJS6 Type 'void' is not assignable to type 'ObservableInput<{}>' 类型“布尔”不能分配给类型“字符串” - Type 'boolean' is not assignable to type 'string'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM