问题标签 [candeactivate]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
2440 浏览

angular - Angular canDeactivate 不起作用并且值显示为 null

我正在关注来自:https ://www.concretepage.com/angular-2/angular-candeactivate-guard-example 的链接,在我的现实世界应用程序中没有什么对我有用。我有以下代码。

应用模块

对话服务

应用路由模块

PendingChangesGuard

注意:我正在使用模板驱动表单和PendingChangesGuard类的方法canDeactivate(component: CanComponentDeactivate...),其中component变量总是为空

0 投票
1 回答
1343 浏览

angular - How to make Angular canDeactivate Service wait for Modal Dialog response?

I have a function inside the component to check if I can leave the route by evaluating a form status. If the status matches to a contidition which I need to ask to leave the route, it shows up a dialog to confirm or cancel. I call this function inside my canDeactivate service, but it triggers the response before waiting for the dialog confirmation.

I've already put a console.log inside the canDeactivate service with the component's function. When a confirmation is not required, it shows true as expected. But on the other hand, when a confirmation is required, it shows undefined before the dialog box appears.

canDeactivateService.ts

example-component.ts

I'd like the service to wait for the confirmation. How could I do this?

0 投票
1 回答
2210 浏览

angular - Angular 8 中的路由器防护

我创建了一个用户输入数据的应用程序。在那个应用程序上,我想实现路由器保护以拒绝用户返回页面,这样他们就不会丢失数据。如果用户单击浏览器上的后退按钮,它会重新加载页面而不是返回?

我正在考虑使用 canDeactivate 拒绝访问上一页和 Angular Location 来确定用户在哪个页面上,然后重新加载该页面。但我不知道如何实现这一点。

0 投票
1 回答
639 浏览

angular - Angular8 RXJS CanDeactivate 在停用前等待可观察的结果

我正在尝试使用 CanDeactivate 防护来检测用户何时离开组件。目的是 #1 检查当前用户是否拥有记录上的“正在编辑”锁,如果是 #2,则调用 observable 来更新数据库。

我拥有的代码部分工作,但有一个竞争条件,锁并不总是被释放,可能是因为在this.updateIsBeingEdited$(false)调用下一个 return 语句时订阅并不总是完成。所以我知道这没有正确实施。

如何使this.updateIsBeingEdited$可观察对象在 canDeactivate 返回值之前完成?

返回值应始终为 true,因为组件应始终停用,它只需要确保this.updateIsBeingEdited$.subscribe()在停用完成之前完成。

零件

警卫

0 投票
0 回答
406 浏览

angular - 带有 Angular 的离子警报 canDeactivate

我正在尝试将 ionic-alert 与 Angular deactivate 一起使用,以保持“确认”功能的行为。

使用此代码,当我单击更改页面时,页面已更改,然后出现确认对话框。

当我使用 confirm() 而不是这个函数时,效果很好,页面等待答案。

如何获得与窗口确认功能相同的行为?

0 投票
1 回答
195 浏览

angular - 动态 canDeactivate Guard:在离开带有更改的表单之前显示确认消息

我正在处理一个有很多表单页面的项目,我想在最终用户尝试导航到另一条路线而不保存更改时向他们提供提示。在所有页面中,我使用的是这样的反应形式

那么我可以让一个可以为我的所有组件停用Guard 的东西吗?

是否可以对所有页面进行相同的禁用保护以防止表单更改?

提前致谢。

0 投票
1 回答
502 浏览

angular - 按下浏览器后退按钮后,CanDeactivate 防护未按预期运行

我已经实现了 canDeactive 保护,一旦点击浏览器按钮,它就会弹出一个模型并询问是/否。单击否后,它会停留在同一页面上,如果我再次按下后退按钮,则会出现弹出窗口,单击否/是按钮后,它会重定向到浏览器历史记录中的空白页面 url,而不是停留在相同的 url 上。

请找到停用守卫的代码。

0 投票
1 回答
3232 浏览

angular - Angular 8 中的页面导航不会触发 canDeactivate()

当从当前组件导航到不同的组件时,我需要向用户抛出一个弹出窗口。

根据用户的输入,我必须要么让他离开,要么让他留下。

经过大量谷歌搜索后,我发现使用实现 canDeactivate 接口的路由保护是解决此问题的最佳方法。

然而,即使在逐步实现了防护之后,在防护中实现的 canDeactivate() 方法也不会在远离我的组件的任何导航上触发。

这是我作为服务实施的警卫。

SearchComponent 是我正在尝试实现 Guard 的组件。(在离开这个组件时,我想抛出一个弹出提示)

这是我的 AppRoutingModule

从 searchcomponent 导航到任何其他组件时,canDeactivate 方法甚至不会被触发。

这是我的组件中的代码。

甚至我的组件中的调试器也不会被触发。

有人可以帮我解决我需要纠正的问题吗?(我只是Angular的初学者,但我觉得这个问题是否与routingmodule中的loadchildren()有关)

0 投票
2 回答
270 浏览

angular - Angular CanDeactivateGuard:如何等待 Obersavble 的正确值或下一个值?

我有一个发出事件的服务:

我还有一个CanDeactivateGuard由组件中的函数触发的:

现在这一切正常。但我有一个问题:

这将始终返回最后一个事件。所以当什么都没有发生时,它会init立即发送。如果update()被调用,它将update直接发送。

我怎样才能使它工作,以便它:

  • ... 等到acceptdecline正在发送?
  • ... 等到下一个新事件发出?
0 投票
1 回答
35 浏览

angular - CanDeactivateGuard 不适用于确认订阅

我需要创建 canDeactiovateGuard 以防止用户在尝试离开表单页面时丢失表单数据。下面的代码不起作用,因为 return canDeactivate 在 canDeactivate = 确认之前起作用。我不知道如何解决这个问题。