1
私はAngular 2で新しくデモアプリケーションを作成しています。 same.Iをしながら、私は以下のコードで何か間違ったことをやっていると思います。このエラーを得た:プロパティ 'path'はタイプ2の 'Router'には存在しません。
canLoad(router: Router): boolean {
let url = `/${router.path}`;
return this.checkLogin(url);
}
私が間違っているの何、私を提案してください?上記のコードでは、path
はrouter
というプロパティではありませんが、私は知っていますが、同じものの正しいプロパティは何ですか?
完全なコード -
import { Injectable } from '@angular/core';
import {
CanActivate, Router,
ActivatedRouteSnapshot,
RouterStateSnapshot,
CanActivateChild,
NavigationExtras,CanLoad
} 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(router: Router): boolean {
let url = `/${router.path}`;
return this.checkLogin(url);
}
checkLogin(url: string): boolean {
if (this.authService.isLoggedIn) { return true; }
// Store the attempted URL for redirecting
this.authService.redirectUrl = url;
// Create a dummy session id
let sessionId = 123456789;
// Set our navigation extras object
// that contains our global query params and fragment
let navigationExtras: NavigationExtras = {
queryParams: { 'shubhamsession_id': sessionId },
fragment: 'shubhamanchor'
};
// Navigate to the login page with extras
this.router.navigate(['/login'], navigationExtras);
return false;
}
}
おかげ@Seidme、しかし、ときに私のコンソール、console.log(ルーター);コンソール上にパスが表示されていますが、console.log(router.url);これは未定義を示しています。なぜこれ? ただし、pathはプロパティであり、urlはプロパティではありません。なぜそれが続くの? –
何が起こっているのかはっきりしませんが、ActivatedRouteSnapshotまたはRouterStateSnapshotを注入することを望む目的を達成するために採用された方法はhttps://angular.io/docs/ts/latest/guide/router.htmlです –