如何限制用户在角应用中直接加载网址

问题描述 投票:0回答:4

有网址说:

学生/学生资料/:ID

我不希望用户直接访问此网址。相反,如果他们尝试直接加载它,我想导航到'学生/我的学生'。

为此,我创建了一个名为previousRouteService的服务:

    import { Injectable } from '@angular/core';
    import { Router, RouterEvent, NavigationEnd } from '@angular/router';


    @Injectable()
    export class PreviousRouteService {

      private previousUrl: string = undefined;
      private currentUrl: string = undefined;

      constructor(private router : Router) {
        this.currentUrl = this.router.url;
        router.events.subscribe(event => {
          if (event instanceof NavigationEnd) {        
            this.previousUrl = this.currentUrl;
            this.currentUrl = event.url;
          };
        });
      }

      public getPreviousUrl(){
        return this.previousUrl;
      }

  canNavigateToProfile()
  {
    console.log('this.previousUrl', this.previousUrl)
    if(this.previousUrl === 'students/my-students')
    {
      return true
    }
    else
    {
      this.router.navigate(['students/my-students']);
      return false;
    }
  }


    }

然后在src / app / guards / post-login / student-profile / student-profile.gaurd.ts中创建了一个后卫:

import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import { PreviousRouteService } from '@app/services';

@Injectable()
export class StudentProfile implements CanActivate {

  constructor(private previousRoute: PreviousRouteService
    ) { }

    canActivate( next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {

      return this.previousRoute.canNavigateToProfile();
    }
}

在lazyloaded模块文件中:

     path: 'student-profile/:id',
        canActivate: [StudentProfile],
        loadChildren: 'app/views/student-post-login/student-profile/student-profile.module#PatientProfileModule'

@NgModule({
  imports: [
  ......
  ......
  providers: [studentProfile]
})

但是当我尝试通过我的学生路线导航到student-profile /:id路线时:

core.js:1448 ERROR错误:未捕获(在承诺中):错误:StaticInjectorError(AppModule)[studentProfile - > PreviousRouteService]:StaticInjectorError(Platform:core)[studentProfile - > PreviousRouteService]:NullInjectorError:没有PreviousRouteService的提供者!错误:StaticInjectorError(AppModule)[studentProfile - > PreviousRouteService]:StaticInjectorError(Platform:core)[studentProfile - > PreviousRouteService]:NullInjectorError:没有PreviousRouteService的提供者!

如果我从患者配置文件gaurd中删除previousRouteService的使用,则此错误消失。

可能是这个错误的原因是什么是实现这种限制的最佳方式,用户可以通过url xyz导航到url abc,应该导航到xyz。

angular angular-routing
4个回答
1
投票

将PreviousRouteService添加到AppModule的提供者(app.module.ts)

@NgModule({
  imports: [
  ......
  ......
  providers: [studentProfile, PreviousRouteService  ]
})

0
投票

如果您使用的是角度6,那么请使用下面的代码,或者如果您使用的是以下版本,则yu必须在Providers中添加特定模块,

import { Injectable } from '@angular/core';
import { Router, RouterEvent, NavigationEnd } from '@angular/router';


@Injectable({
    providedIn: 'root'
})


 export class PreviousRouteService { }

0
投票

如果我理解正确,你必须声明你正在使用它并导出的提供者,然后你必须将它声明为你想要使用它的导入。

例:

exampleCreate.module:

@NgModule {
providers: [yourservice],
exports: [yourservice]
}

exampleWhereUse.module:

@NgModule {
imports: [exampleCreate.module]
}

0
投票

我不确定我是否理解你的问题。但是如何检查构造函数中的id是否为null? if为null然后重定向到我的学生?

© www.soinside.com 2019 - 2024. All rights reserved.