【问题标题】:Angular - router.navigate() not redirecting to target pageAngular - router.navigate() 未重定向到目标页面
【发布时间】:2017-06-18 14:50:19
【问题描述】:

我正在做一个简单的登录过程,我尝试保护某些路径,除非它们经过身份验证。

app.routing.ts

    const  appRoutes: Routes = [
    {
      path: 'add-merchant-admin',
      component : AddMerchantAdminComponent,
      canActivate : [AuthGard]
    },
    {
      path: 'list-merchant-admin',
      component : ListMerchantAdminComponent,
      canActivate : [AuthGard]
    },
    {
      path: 'login',
      component : LoginComponent
    },
    {
      path: '**',
      component: NotFoundComponent
    }
];

export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);

authGard.service.ts

  import { Injectable } from '@angular/core';
  import {CanActivate, Router} from "@angular/router";
  import {AuthenticationService} from "../authentication-service/authentication.service";

  @Injectable()
  export class AuthGard implements CanActivate {

    constructor(private _authService:AuthenticationService, private _router:Router) { }

    canActivate() {
      if(this._authService.isLoggedIn)
        return true;

      this._router.navigate(['login']);
      return false;
    }
  }

身份验证服务

    @Injectable()
    export class AuthenticationService {

      isLoggedIn = false;

      constructor() {
      }

      login(){
         this.isLoggedIn = true;
      }

      logout(){
        this.isLoggedIn = false;
      }
    }

当我尝试访问 受保护的路径(如 add-merchant-admin)时,浏览器会不断加载页面,消耗大量内存,直至冻结。 p>

这些是关于我的应用的详细信息。

节点:6.10.2

操作系统:win32 x64

@angular/动画:4.2.3

@angular/common: 4.2.3

@angular/编译器:4.2.3

@angular/core: 4.2.3

@angular/forms: 4.2.3

@angular/http: 4.2.3

@angular/material: 2.0.0-beta.6

@angular/platform-b​​rowser: 4.2.3

@angular/platform-b​​rowser-dynamic: 4.2.3

@angular/路由器:4.2.3

@angular/cli: 1.0.1

@angular/compiler-cli: 4.2.3

已验证依赖注入。

组件已正确导入。

我不知道这个应用程序是怎么回事,通常它应该可以工作。

希望大家能帮帮我。

非常感谢。

【问题讨论】:

    标签: javascript angular routing angular-ui-router


    【解决方案1】:

    将 AuthGuard 更改为:

      import { Injectable } from '@angular/core';
      import {CanActivate, Router} from "@angular/router";
      import {AuthenticationService} from "../authentication-service/authentication.service";
    
      @Injectable()
      export class AuthGard implements CanActivate {
    
        constructor(private _authService:AuthenticationService, private _router:Router) { }
    
        canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
          if(this._authService.isLoggedIn)
            return true;
    
          this._router.navigate(['/login']);
          return false;
        }
      }
    

    在导航方法的参数数组的第一个参数中使用/ 作为前缀,您可以告诉 Angular 路径是绝对路径(从根开始)。

    【讨论】:

      【解决方案2】:

      如下修改路由,

      const  appRoutes: Routes = [
          {
            path: 'add-merchant-admin',
            component : AddMerchantAdminComponent,
            canActivate : [AuthGard]
          },
          {
            path: 'list-merchant-admin',
            component : ListMerchantAdminComponent,
            canActivate : [AuthGard]
          },
          {
            path: 'login',
            component : LoginComponent
          },
          {
            path: 'notfound',
            component :NotFoundComponent
          },
          {
            path: '',
            redirectTo: 'login',
            pathMatch: 'full'
          },
          {
            path: '**',
            redirectTo: 'notfound',
            pathMatch: 'full'
          },
      ];
      

      【讨论】:

      • 为什么你认为问题与路由的定义有关?
      • 默认情况下,您拥有该路由定义的路径为空。
      • 是的,但这会导致应用程序总是在第一次加载时呈现 NotFoundComponent,而不是在应用程序中冻结
      猜你喜欢
      • 2020-01-11
      • 2022-01-11
      • 2021-01-28
      • 2020-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-27
      • 1970-01-01
      相关资源
      最近更新 更多