相对路由在canActivate Guard中不起作用

时间:2019-04-26 11:32:02

标签: angular6

import { AdminAuthService } from './admin-auth.service';
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router, Route, ActivatedRoute } from '@angular/router';
import { Observable } from 'rxjs';

@Injectable()
export class AdminAuthGuard implements CanActivate {


    constructor(private _authService: AdminAuthService, private _router: Router, private acRoute: ActivatedRoute) {
    }

    canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
        return new Promise((resolve, reject) => {
            this._authService.checkValidUser().then((resp) => {
                //let url = state.url.substr(0, state.url.lastIndexOf("/"));
                !resp && this._router.navigate(['../adminlogin'], {relativeTo: this.acRoute});
                //!resp && this._router.navigate([url, "adminlogin"]);
                resolve(resp);
            });
        })
    }
}

我遇到以下错误 错误错误:未捕获(承诺):错误:无法匹配任何路由。网址段:“ adminlogin” 错误:无法匹配任何路线。网址段:“ adminlogin”

路由代码如下

{
        path: "admin", component: AdminComponent,
        children: [
            { path: '', redirectTo: 'adminlogin', pathMatch: "full" },
            { path: 'adminlogin', component: LoginComponent },
            { path: 'list', component: ListComponent, canActivate: [AdminAuthGuard] }
        ]
    }

有人可以帮我吗?

2 个答案:

答案 0 :(得分:0)

这就是说您没有与adminLogin匹配的路线,请更新您的可激活路线

!resp && this._router.navigate(['adminlogin'], {relativeTo: this.acRoute});

答案 1 :(得分:0)

似乎您没有与adminLogin匹配的路径。

请在canActivate方法中添加以下内容:-

for(auto lIter = lLines.begin(), rIter = rLines.begin(); liter != lLines.end(); ++lIter, ++rIter)
{
   ... 
}