0
我想在我的應用程序中添加一些受限制的路線。我寫了一個警衛像他們在這裏的文檔說明,但我嘗試去實現它沒有打字稿:https://angular.io/docs/ts/latest/guide/router.html#!#guardsAngular2 CanActivate undefined
我的後衛是這樣,但我不能使用CanActivate,因爲它不是在@angular/router
V3.1.1
import { Router, CanActivate } from '@angular/router';
import { AuthService } from './app.auth.service';
console.log(CanActivate); // undefined
export class AuthGuard extends CanActivate {
constructor(AuthService) {
super();
this._authService = AuthService;
}
canActivate() {
return this.checkIfLoggedIn();
}
_checkIfLoggedIn() {
const user = this._authService.getUser();
return user;
}
static get parameters() {
return [[AuthService]];
}
}
任何想法?
編輯
這種方法按預期工作,感謝@PierreDuc的幫助我回到賽場上。
import { Router } from '@angular/router';
import { AuthService } from './app.auth.service';
export class AuthGuard {
constructor(AuthService, Router) {
this._authService = AuthService;
this._router = Router;
}
canActivate() {
if (this._authService.isLoggedIn()) {
return true;
}
this._router.navigate(['/login']);
return false;
}
static get parameters() {
return [[AuthService], [Router]];
}
}
好的,謝謝。接口是關鍵字。接口不是ES6的一部分,對吧?!你有一個想法如何使用es6和沒有TypeScript的guard/canActive概念? –
@ d-bro82沒有必要擴展它來啓用任何方法,與在TypeScript中工作的方式一樣,它也應該在ES6中工作。 – PierreDuc
知道了!這非常有幫助,ty –