【问题标题】:Angular project forget password send email link not working in Chrome browser (First time only. Second time working as expected)Angular 项目忘记密码发送电子邮件链接在 Chrome 浏览器中不起作用(仅第一次。第二次按预期工作)
【发布时间】:2020-08-06 09:39:49
【问题描述】:

我正在开发一个 angular6 应用程序并使用域石灰“www.mydomain.com/”进行部署。

所有路由都运行良好,例如“www.mydomain.com/dashboard”、“www.mydomain.com/products”等,

问题是我们有一个更改密码来链接它,就像“www.mydomain.com/mga/sps/authsvcurn:ibm:security:authentication/changePassword”。 (链接是发送 OTP 邮件到我们的邮件 id 用于登录应用程序并等待更新 OTP)

我使用链接重定向更改密码。但它不会重定向它只会转到主页“www.mydomain.com/”,因为路由相同的域。

更新

此问题仅发生在仅限首次使用 chrome 浏览器。当我第一次登录时点击“更改密码”按钮,链接被触发,但几秒钟后重定向到主页。

如果我第二次单击同一个按钮,链接将重定向到正确的页面(更改密码 URL 页面)。

我该如何解决这个问题?请帮帮我。

代码如下:

my-profile.component.html

<a (click)="changePassword()">Change Password</a>

my-profile.component.ts

changePassword() {     window.location.href = 'www.mydomain.com/mga/sps/authsvcurn:ibm:security:authentication/changePassword';   }

【问题讨论】:

  • 能否也显示路由文件中的路径?尤其是应该匹配 changePassword 组件的那个?
  • Angular 没有任何“更改密码”组件。该链接与其他团队提供的某些外部 URL 不同。
  • 打开网络调试器,检查是否有重定向或错误。你的mga/sps/... 页面是有角度的路线吗?

标签: angular routes angular6


【解决方案1】:

您是否已经尝试过 Angular 路由器?

import { Router } from '@angular/router';

constructor(
    private router: Router
  ) { }

changePassword() {
    this.router.navigate(['/myURL', { myParamName: myParamValue }]);
  }

在目标组件中:

import { ActivatedRoute, ParamMap } from '@angular/router';

  constructor(
    private route: ActivatedRoute
  ) { }

public ngOnInit() {
    this.route.paramMap
      .subscribe((params: ParamMap) => {
        const myParamValue = params.get('myParamName');
        if (myParamValue) {

        }
    });
}

【讨论】:

    【解决方案2】:

    您可以将changePassword() 方法更改为:

    <a (click)="changePassword($event)">Change Password</a>
    
    
    changePassword(event) {
        window.location.href = 'www.mydomain.com/mga/sps/authsvcurn:ibm:security:authentication/changePassword';
        event.preventDefault();
     }
    

    您也可以使用window.open:

    // _blank is optional, it opens the url in a new window. 
    // You can omit if you want to navigate from the current window.
    
    window.open("https://google.com/","_blank");
    


    更新

    其他可以帮助你调试的点​​:

    1 - 启用路由跟踪(app-routing.module.ts):

    imports: [
        RouterModule.forRoot(
          routes,
          { enableTracing: true } // <-- for debugging purposes only
        )
    ]
    

    这将在 DevTools 控制台上打印 Angular 访问的所有路由。
    这为您提供了一些见解,为什么第一次不工作。

    我可能猜到,目标路由或所需参数未正确定义,Angular 路由无法将其与任何配置的路径匹配,重定向到 /home。

    2 - 如果 Stackblitz 解决方案不可行,您能否提供所有涉及的路由以及触发重定向时涉及的所有相关参数?

    【讨论】:

    • 它不工作。首次登录应用程序后,单击“更改密码”按钮将重定向到主页,但再次单击按钮将重定向到正确的 URL(忘记密码链接)
    • 我更新了我的答案。万一新方法对你不起作用,我建议提供路由路径和涉及的其他代码部分,或者创建一个 stackBlitz 演示。
    • 我试过但由于某种原因无法工作我无法创建 stackBlitz
    • 我用一个提示更新了我的答案,需要更多信息。
    猜你喜欢
    • 1970-01-01
    • 2017-12-31
    • 2013-08-13
    • 1970-01-01
    • 2016-08-31
    • 2019-12-03
    • 2013-06-24
    • 2015-10-21
    • 2018-08-17
    相关资源
    最近更新 更多