首页 > 解决方案 > 角度返回 url 为空

问题描述

在我的应用程序中,如果人员未登录,则使用返回 url 登录

 this.user=this._session.getEmployee();
 if(!this.user){
    this.router.navigate(['PA/Login'], {queryParams: {returnUrl:this.url}});
 }

在登录组件中,ngOnInit我正在尝试使用下面的代码。在我声明的构造函数中private route: ActivatedRoute

this.url=this.route.snapshot.paramMap.get('returnUrl');

这是我的路线配置

const routes: Routes = [
  {path:"",redirectTo:"PA/Login",pathMatch:"full"},
  {path:"PA",redirectTo:"PA/Login",pathMatch:"full"},
  {path:"PA/Home",component:HomeComponent}, 
  {path:"PA/Email/:url",component:EmailComponent}, 
  {path:"PA/Login",component:LoginComponent}, 
  {path:"PA/Login?returnUrl=:returnUrl",component:LoginComponent}, 
];

但它返回null。我在哪里犯错?

谢谢你的帮助

标签: angular

解决方案


您正在尝试从 获取查询参数paramMap,当您应该通过时queryParamMap

this.route.snapshot.queryParamMap.get('returnUrl');

此外,您不需要路由器配置中的以下行:

{path:"PA/Login?returnUrl=:returnUrl",component:LoginComponent},


推荐阅读