Angular 如何将订阅内容传递到CanDeactivateGuard
我正在为我的路由使用CanDeactivateGuard,我希望将数据传递给它。卫士只是提示用户保存任何未保存的更改。它启动一个模式对话,我想传递到对话中的数据来自订阅 我用这个答案来指导我的问题: …但就我所知,我的做法似乎有点过于简单,因为我无法通过我的路线传递订阅 这是我的警卫:Angular 如何将订阅内容传递到CanDeactivateGuard,angular,typescript,Angular,Typescript,我正在为我的路由使用CanDeactivateGuard,我希望将数据传递给它。卫士只是提示用户保存任何未保存的更改。它启动一个模式对话,我想传递到对话中的数据来自订阅 我用这个答案来指导我的问题: …但就我所知,我的做法似乎有点过于简单,因为我无法通过我的路线传递订阅 这是我的警卫: export class PendingChangesGuard implements CanDeactivate<ComponentCanDeactivate> { constructor(
export class PendingChangesGuard
implements CanDeactivate<ComponentCanDeactivate> {
constructor(
private myDialogService: MyDialogService,
) {}
canDeactivate(
component: ComponentCanDeactivate,
currentRoute: ActivatedRouteSnapshot,
currentState: RouterStateSnapshot,
nextState?: RouterStateSnapshot
): boolean | Observable<boolean> {
return component.canDeactivate() || this.openConfirmDialog();
}
openConfirmDialog() {
const ref = this.myDialogService.open(MyDialogComponent, /** Pass a CONFIG object here **/);
return ref.afterClosed$();
}
}
此myContentSubscription
将填充打开模式时需要传递的数据。我的假设是我的警卫不应该执行对该服务的调用,这不是警卫的责任。另外,因为调用是异步的,所以我必须等待才能将数据传递到模式
哪里是放置订阅代码的正确位置,以便警卫在调用时可以访问该代码?希望这能帮助遇到此问题的其他人-- 应用程序加载时,app.component.ts调用HomePageTridOnContentService。这意味着,当我的守卫开始工作时,服务已经检索到数据,我可以在守卫中简单地执行以下操作:
openConfirmDialog() {
const modalData =
this.homePageContentService.getModalContent();
const config = { data: { ...modalData } };
const ref = this.modalDialogService.open(ModalDialogComponent,
config);
return ref.afterClosed$();
}
请不要破坏你的帖子,为别人做更多的工作。通过在Stack Exchange网络上发布,您已授予Stack Exchange在下不可撤销的权利,以分发该内容(即,无论您未来的选择如何)。根据堆栈交换策略,帖子的非破坏版本是分发的版本。因此,任何故意破坏行为都将恢复原状。如果您想了解有关删除帖子的更多信息,请参阅:
openConfirmDialog() {
const modalData =
this.homePageContentService.getModalContent();
const config = { data: { ...modalData } };
const ref = this.modalDialogService.open(ModalDialogComponent,
config);
return ref.afterClosed$();
}