Angular 选中subscribe值并在canDeactivate中返回它
在canDeactivate guard中,我订阅确认服务。确认组件有3个按钮,单击其中一个按钮时,它将弹出枚举。如何在subscribe to view中检查值,根据值执行smth并返回布尔值 CanDeactivateGuard:Angular 选中subscribe值并在canDeactivate中返回它,angular,rxjs,Angular,Rxjs,在canDeactivate guard中,我订阅确认服务。确认组件有3个按钮,单击其中一个按钮时,它将弹出枚举。如何在subscribe to view中检查值,根据值执行smth并返回布尔值 CanDeactivateGuard: export class CanDeactivateGuard implements CanDeactivate<CanComponentDeactivate> { constructor(private modalDialogServic
export class CanDeactivateGuard implements CanDeactivate<CanComponentDeactivate> {
constructor(private modalDialogService: ModalService) {
}
canDeactivate(component: CanComponentDeactivate,
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | boolean {
if (!component.isUnsavedChanges()) {
return true;
}
this.modalDialogService.showConfirm(ConfirmType.CAN_DEACTIVATE).subscribe(val => {
//here I want to check value, do some functions and return true/false
})
}
}
导出类CanDeactivateGuard实现CanDeactivate{
构造函数(专用modalDialogService:ModalService){
}
canDeactivate(组件:CanComponentDeactivate,
路由:ActivatedRouteSnapshot,
状态:RouterStateSnapshot):可观察的|布尔值{
如果(!component.isUnsavedChanges()){
返回true;
}
this.modalDialogService.showConfirm(ConfirmType.CAN_DEACTIVATE).subscribe(val=>{
//这里我想检查值,执行一些函数并返回true/false
})
}
}
确认组件:
export class ConfirmComponent {
subject: Subject<ConfirmAction>;
constructor(public bsModalRef: BsModalRef) {}
action(action: ConfirmAction):void { //here the value of click
this.bsModalRef.hide();
this.subject.next(action);
this.subject.complete();
}
}
export enum ConfirmAction {
SAVE,
REVERT,
CANCEl,
}
导出类确认组件{
主题:主题;
构造函数(公共bsModalRef:bsModalRef){}
action(action:confirmation):void{//此处是单击的值
this.bsModalRef.hide();
本。主题。下一步(行动);
this.subject.complete();
}
}
导出枚举确认{
拯救
还原
取消,
}
ModalService:
export class ModalService {
constructor(private bsModalService: BsModalService,
private ts: TranslatorService) {}
public showConfirm(modalDialogType: ConfirmType, extraInfo?: string): Observable<ConfirmAction> {
let modal;
switch (modalDialogType) {
case ConfirmType.CAN_DEACTIVATE: {
modal = this.bsModalService.show(ConfirmComponent, config);
break;
}
/*
other cases
*/
return modal.content.subject.asObservable();
}
导出类ModalService{
构造函数(专用bsModalService:bsModalService,
私人ts:TranslatorService{}
public showConfirm(modalDialogType:ConfirmType,extraInfo?:字符串):可观察{
让模态;
开关(modalDialogType){
案例确认类型。是否可以停用:{
modal=this.bsModalService.show(ConfirmComponent,config);
打破
}
/*
其他情况
*/
返回modal.content.subject.asObservable();
}
canDeactivate正在等待一个可观察到的,您可以这样做:
canDeactivate(component: CanComponentDeactivate,
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | boolean {
if (!component.isUnsavedChanges()) {
return true;
}
return this.modalDialogService.showConfirm(ConfirmType.CAN_DEACTIVATE).pipe(
map(val => {
// Here, do your work and return true or false
})
)
}
canDeactivate(组件:CanComponentDeactivate,
路由:ActivatedRouteSnapshot,
状态:RouterStateSnapshot):可观察的|布尔值{
如果(!component.isUnsavedChanges()){
返回true;
}
返回此.modalDialogService.showConfirm(ConfirmType.CAN\u DEACTIVATE).pipe(
映射(val=>{
//在这里,做你的工作,并返回正确或错误
})
)
}
看看Rxjs文档
我希望它能帮助你