LoggedInOutlet angular2 身份验证 - 路由器 v3.0.0-alpha8 - ComponentInstruction 在哪里? [英] LoggedInOutlet angular2 authentication - Router v3.0.0-alpha8 - Where is ComponentInstruction?

查看:20
本文介绍了LoggedInOutlet angular2 身份验证 - 路由器 v3.0.0-alpha8 - ComponentInstruction 在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这样的代码来扩展 RouterOutlet 并创建应用程序范围的身份验证和路由保护

I am using code like this to extend RouterOutlet and create app wide authentication and route protection

import {Directive, Attribute, ViewContainerRef, DynamicComponentLoader} from '@angular/core';
import {Router, ComponentInstruction} from '@angular/router';
import {Router} from '@angular/router';
import {RouterOutletMap} from '@angular/router/src/router_outlet_map';
import {RouterOutlet} from '@angular/router/src/directives/router_outlet';

import {Authentication} from '../common/authentication.service';

@Directive({
  selector: 'router-outlet'
})
export class LoggedInRouterOutlet extends RouterOutlet {
  publicRoutes:any;
  isAuthenticated:boolean;
  //private router: any;

  constructor(public _elementRef: ElementRef, public _loader: DynamicComponentLoader,
              public _parentRouter: Router, @Attribute('name') nameAttr: string, public authService:Authentication) {
    super(_elementRef, _loader, _parentRouter, nameAttr);
    this.isAuthenticated = authService.isLoggedIn();


    //this.router = _parentRouter;
    /**
     * DEFINE PUBLIC ROUTES
     *
     * The Boolean following each route below denotes whether the route requires authentication to view.
     *
     * Format: key/value pair
     * - key is the /route url "/login", "/signup", etc
     * - value is a boolean true/false
     *    `true` means it's a publicly available route. No authentication required
     *    `false` means it's a protected route which is hidden until user is authenticated
     *
     */
    this.publicRoutes = {
      'login': true,
      'signup': true,
      '404': true
    };
  } // end constructor

  routeIsActive(routePath:string) {
    return this.router.url == routePath;
  }

  activate(instruction: ComponentInstruction) {
    // let url = instruction.urlPath;
    let url = this.router.url;
    // If the url doesn't match publicRoutes and they are not authenticated...
    if (!this.publicRoutes[url] && !this.isAuthenticated) {
      // todo: redirect to Login, may be there a better way?
      this.router.navigateByUrl('/login');
    }
    return super.activate(instruction);
  }
}

问题是新的v3.0.0-alpha8路由器中不存在ComponentInstruction,并且超级方法签名发生了变化.如何更新它以在新路由器中工作?我找不到任何解释更改的文档.

Problem is that ComponentInstruction does not exist in the new v3.0.0-alpha8 router, and the super method signature has changed. How do I update this to work in the new router? I cannot find any documentation explaining the changes.

推荐答案

ComponentInstruction 已被弃用.在 Angular2 的当前 RC4 版本中,此类已被列在 reouter-deprecated 下.随着 RC5 的到来,这个包将被删除.

ComponentInstruction has been deprecated. In the current RC4 version of Angular2, this class has been listed under reouter-deprecated. With RC5 coming in, this package would be dropped.

RouterOutlet 随着时间的推移发生了很大变化,为了让您的类 LoggedInRouterOultet 工作,您必须使用 CanActivate 界面.

RouterOutlet has changed a lot over time and to make your class LoggedInRouterOultet work, you have to use CanActivate interface.

你可以这样做:

有一个像 LoggedInActivator 这样的可注入服务显示在这里:

import { Injectable } from '@angular/core';
import { Router, CanActivate } from '@angular/router';
import { LogInService } from './login.service';

@Injectable()
export class LoggedInActivator implements CanActivate {
  constructor(private loginService: LogInService) {}

  canActivate() {
    return this.loginService.isLoggedIn();
  }
}

在定义路由时添加canActivate并将其映射到组件上的LoggedInActivator:

{ path: 'home', component: HomeComponent, canActivate: [LoggedInActivator] }

我希望这会有所帮助!

这篇关于LoggedInOutlet angular2 身份验证 - 路由器 v3.0.0-alpha8 - ComponentInstruction 在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆