angular2:如何在维护重定向 URL 的同时获取 CanLoad 防护的完整路径

2024-01-04

我正在使用 Angular 2.4 版本和路由器版本“^3.4.10”。

我正在尝试使用 authguard 服务处理重定向 url。

当用户点击 url“domain/assignment/3/detail”时,如果用户未登录,则用户将重定向到“domain/login”页面。 当用户成功登录系统时,然后重定向到“domain/assignment/3/detail”用户尝试访问的上一个 url。

我已经在分配模块上实现了 CanLoad 防护。因此,当用户尝试访问 url 'domain/assignment/3/detail' 且用户未登录时,url 会存储到 authservice 的 redirectUrl 属性中 (this.authService.redirectUrl)。

所以这就是我的情况的问题。我无法获取用户点击的网址的完整路径。 我在 CanLoad 防护中收到“分配”而不是“分配/3/详细信息”。 我怎样才能获得完整路径,以便我可以将用户重定向到 CanLoad 防护中的正确路径。

CanLoad:

 canLoad(route: Route): boolean {

            let url = `/${route.path}`; // here i got url path 'assignment' instead 'assignment/3/detail'

            return this.checkLogin(url);
        }

主要路由应用程序.routes.ts

const routes: Routes = [
{ path: '', redirectTo: 'login', pathMatch: 'full' },
{ path: 'login', component: LoginComponent},
{
    path: 'assignment',
    loadChildren: './assignment/assignment.module#AssignmentModule',
    canLoad: [AuthGuard]
},
{ path: '**', redirectTo: '', pathMatch: 'full' }];

分配路由:分配路由.ts

       const assignmentRoutes: Routes = [
        {
            path: '',
            component: AssignmentComponent,
            canActivate: [AuthGuard]
            children: [
                {
                    path: '',
                    canActivateChild: [AuthGuard],
                    children: [
                        {
                            path: ':assignmentId/detail', component: AssignmentDetailComponent,
                            canActivate: [AuthGuard]

                        }
                      ]
                  }]
         }];

AuthGuard:auth-gurad.service.ts

import { Injectable } from '@angular/core';
import {
    CanActivate, Router,
    ActivatedRouteSnapshot,
    RouterStateSnapshot,
    CanActivateChild,
    NavigationExtras,
    CanLoad, Route
} from '@angular/router';
import { AuthService } from './auth.service';

@Injectable()
export class AuthGuard implements CanActivate, CanActivateChild, CanLoad {
    constructor(private authService: AuthService, private router: Router) { }

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
        let url: string = state.url;
        return this.checkLogin(url);
    }

    canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
        return this.canActivate(route, state);
    }

    canLoad(route: Route): boolean {

        let url = `/${route.path}`; // here i got url path 'assignment' instead 'assignment/3/detail'

        return this.checkLogin(url);
    }



     checkLogin(url: string): boolean {
            if (this.authService.isLoggedIn) {
                if(this.authService.redirectUrl!=null){
                    let redirectUrl = this.authService.redirectUrl;
                    this.authService.redirectUrl = null;
                    this.this.router.navigate([redirectUrl]);
                }
                return true;
                }

        // Store the attempted URL for redirecting
        this.authService.redirectUrl = url;

        // Navigate to the login page 
        this.router.navigate(['/login']);

        return false;
    }
}

如果你看一下签名可以加载方法 https://angular.io/api/router/CanLoad,还有第二个参数segments您可以使用它来生成 url。

canLoad(route: Route, segments: UrlSegment[]): Observable<boolean> | Promise<boolean> | boolean

您可以使用以下代码生成完整路径:

canLoad(route: Route, segments: UrlSegment[]): boolean {

  const fullPath = segments.reduce((path, currentSegment) => {
    return `${path}/${currentSegment.path}`;
  }, '');

  console.log(fullPath);

}

注意:使用此方法您将获得完整路径,但不会获得任何查询参数。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

angular2:如何在维护重定向 URL 的同时获取 CanLoad 防护的完整路径 的相关文章

  • Angular 如何根据路线更改导航菜单标题

    我正在使用 Angular 4 在当前项目中开发仪表板布局 当用户在应用程序的不同部分之间导航时 我需要更新导航菜单标题标题以反映应用程序的当前部分 例如 当用户访问设置时 页面标题 应更改为 设置 该项目基于 net core 2 Ang
  • 刷新后的Whitelabel错误页面

    我有 Spring Boot 应用程序 后端 对于前端 我使用 Angular 2 单页应用程序 每当我导航到路线 例如 localhost 8080 getAccounts 并在导航后刷新时 我都会收到 Whitelabel 错误页面 如
  • 角度路由器导航然后重新加载

    所以我想在导航到特定路线后重新加载应用程序 我使用 router navigate 根据用户角色导航到特定路线 效果很好 但如果来自登录页面 我必须在路由后重新加载页面 不是每次用户打开该特定路线时 此处重新加载是为了更新页面语言取决于用户
  • 为什么我的node_modules中有@angular/core,但仍然出现此错误?

    我从 angular io 下载了一个示例项目 从这里https angular io generated zips cli quickstart cli quickstart zip https angular io generated
  • 规范化 URL 去掉尾部斜杠

    实施Location normalize在 Angular 2 中 从 URL 中去除尾部斜杠 文档 https angular io docs ts latest api common index Location class html
  • 传递参数 Angular 2 传统方式

    我正在尝试以这种格式将参数传递给一个组件www domain com param value 但是 Angular 不断发送这样的参数www domain com param value 为什么要更换 for 这是我的路线配置 const
  • 当由 router-outlet 创建组件时,如何为组件设置输入绑定?

    当路由器创建组件时 如何设置属性绑定 不支持对路由器添加的组件使用绑定 另请参阅此讨论https github com angular angular issues 4452 https github com angular angular
  • “没有 AuthGuard 提供商!”在 Angular 2 中使用 CanActivate

    EDIT 显然这已经过时了 现在你在providersNgModule 中的数组 查看其他答案或官方文档以获取更多信息 组件上的引导已经过时了 provideRouter 也已经过时了 我正在尝试使用 Angular2 指南中的登录名和 A
  • 如何将不存在的链接重定向到 Angular 2 中的主页?

    如果用户输入不存在的链接 我希望页面重定向到主页 我该怎么做 谢谢 RouteConfig path home name Home component HomeComponent path about name About componen
  • 如何获取Angular2 RC5中的路线参数

    我已将我的 angular2 项目升级为RC5 using angular cli webpack 我提供的路由如下 const appRoutes Routes path project manager component Project
  • Angular2 查询参数订阅触发两次

    尝试处理 OAuth 登录场景 其中如果用户登陆页面authorization code在查询字符串中 我们处理令牌并继续or如果他们在没有该令牌的情况下登陆页面 我们会检查本地存储中是否存在现有令牌 确保其仍然有效 并根据其有效性重定向到
  • Angular 2 路由器路径

    我有两个关于 Angular 2 路由器路径的问题 我花了一些时间在谷歌上搜索它 但没有运气 无论如何我有以下路由设置 path contract component ContractInsertUpdateComponent childr
  • 为什么直接导航到该路线时不匹配?

    郑重声明 这是使用当前相当新的 angular router 3 0 0 alpha 8 路线定义位于帖子底部 当尝试在我的应用程序中导航时 行为会有所不同 具体取决于我是直接输入 URL 还是通过链接输入 Works 进入http loc
  • 使用 Angular 2 连接 Google Maps Nativescript 插件

    我正在尝试使用 Nativescript 的 Google 地图插件 https github com dapriett nativescript google maps sdk https github com dapriett nati
  • 找不到主要出口来加载“XYZComponent”

    我在 Net MVC 组件中加载了 ng2 代码 但控制台中显示以下错误 异常 未捕获 承诺 错误 找不到加载 UsersComponent 的主要出口 错误 找不到加载 UsersComponent 的主要出口 知道可能是什么问题吗 我正
  • 如何在navigationStart路由器事件中暂停路线更改

    在我的应用程序中 我有从包 节点模块 公开的路由 因此 我无法将 canActivate 或 canDeactivate 用于节点模块内定义的路由 因此 我开始订阅应用程序组件中的所有路线更改 并根据条件将用户重定向到不同的路线 由于条件有
  • ng2-select 中的数据绑定失败

    我正在尝试使用 ng2 select 将对象数组绑定到下拉列表 当我尝试使用字符串数组时它工作正常 private category Array value 1 text Table value 2 text Chair value 3 t
  • Angular 2 获取当前路线

    所以我需要以某种方式检查我是否在主页上执行某些操作 而在其他页面上则不执行此操作 该组件也在所有页面上导入 如果我在主页上 如何检测该组件 Thanks 尝试这个 import Router from angular router expo
  • 重定向到 Angular2 中 @CanActivate 内的不同组件

    有什么方法可以重定向到 Angular2 中的 CanActivate 不同的组件吗 截至今天 使用最新的 angular router 3 0 0 rc 1 这里有一些关于如何通过以下方式做到这一点的参考 CanActivate路线守卫
  • 获取路由查询参数

    我正在尝试从 rc1 迁移到 rc4 但在获取查询字符串参数时遇到问题 ActivatedRoute 对象始终为空 英雄组件 ts import Component OnInit from angular core import Contr

随机推荐