当前位置: 首页 > 知识库问答 >
问题:

使用Angular2,如何在登录重定向之前重定向到以前的URL

高正初
2023-03-14

基本上:我如何访问或确定以前请求的URL?

我真的不想求助于传递查询字符串参数,除非我真的不得不这样做。理想情况下,最好能够将< code>history集合作为< code>Router组件的一部分,类似于< code>backbone.history!

共有3个答案

裴欣荣
2023-03-14
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Subscription } from 'rxjs/Subscription';

@Injectable()
export class UrlTrackingService {

  public originalUrl: string;

  constructor(
    private router: Router
  ) {
    let subscription: Subscription = this.router.events.subscribe((val) => {
      this.originalUrl = val.url;
      subscription.unsubscribe();
    });
  }

}
谯和煦
2023-03-14

另一种方法是订阅< code>Location中的< code>popstate事件。MDN的文档讨论了您可能期望获得的价值。

class MyCLass {
  constructor(private location: Location) {
    location.subscribe((val) => {
        // do something with the state that's passed in
    })
  }
}

否则,您可能需要一个服务来跟踪路由器中的变化,以便您可以访问它们。

class MyTrackingService {
  constructor(private router: Router) {
    router.subscribe((val) => {
        // store the routes here so that you can peel them off as needed?
    })
  }
}

在这种情况下,我访问路由器对象并订阅任何更改,以便跟踪它们。

谷泽宇
2023-03-14
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) 
{
    // keep the attempted URL for redirecting
    this._loginService.redirectUrl = state.url;
}

使用路由器成功进行身份验证时重定向到该URL(例如,在login.component.ts中)。E、 g.<code>此._router.navigateByUrl(重定向URL)

另外,@MichaelOryl和@Vitali的建议可能会起作用,但我的方式更符合Angular2最终版本。

 类似资料:
  • 我正在将DNN应用程序从07.00.02升级到07.03.04并在安装后将所有门户重定向到登录页面。所有门户都配置有一个登陆页面,该页面被配置为允许“所有用户”角色视图访问。还有人在升级后遇到过这个问题吗? 2015-02-05 05:45:01 127.0.0.1 GET/-80-127.0.0.1 Mozilla/5.0+(Windows+NT+6.3;+WOW64)+AppleWebKit/

  • 我在Laravel5.2中使用了php artisan函数。 我想重定向的客人登录页面,若客人点击链接,只有用户并没有客人。 我想在登录后将用户重定向到后面的页面。 我怎么能这么做?请详细展示一些文件名示例。 ///////编辑 路线 控制器 Kernel.php

  • 我想在登录到我的索引页面(index.jsp)后进行重定向,实际上我的登录可以正常工作,但ajax支持缺少重定向。 她是我的登录名。jsp 这是我的登录 我的struts配置文件: 对于DAO部分是: 公共字符串验证(字符串用户名,字符串userpass)抛出SQLException,Exception{ 谢谢你的帮助。

  • 记录器文件中的日志- org.springframework.Security.Access.event.loggerlistener-安全授权失败,原因是:org.springframework.Security.Access.accessdeniedexception:访问被拒绝;通过身份验证的主体:org.springframework.security.authentication.ano

  • 我的应用程序看起来像: 如果用户访问 /game下的页面并且没有登录,我想将他们重定向到登录页面。 如何在所有路由器中以优雅的方式进行?

  • 我在模态窗口中有登录表单。成功登录后,用户被重定向到< code>/页面。我正试图找到一种方法,在登录后留在联系页面或另一个页面。如何做到这一点?我的代码是: