我的路线配置如下:
export const routes: Routes = [
{ path: '', redirectTo: 'home', pathMatch: 'full'},
{
path: "register",
component: Abc,
canActivate: [NotAuthGard]
},
{
path: "xyz",
component: Xyz,
canActivate: [AuthGard],
children: [
{ path: "xy1", component: XY1 },
{ path: "xy2", component: XY2 },
]
},
{ path: "pqr", component: Pqr, canActivate: [AuthGard] },
{ path: '**', component: PageNotFoundComponent },
];
我有一些注射服务如下:
import { Injector } from "@angular/core";
import { Router } from "@angular/router";
import { OAuthService } from "../../oAuth/oAuth.service";
@Injectable()
export class ActivityManagerService {
constructor(
private injector: Injector,
private router: Router,
private oAuth: OAuthService
) {
}
goto(to: any[] | any, data?: any) {
if (!to) return;
if(to[0] === '/'){
to = to.substring(1);
}
this.canActivate(to, canIt => {
if (!canIt) {
to = "login";
}
if(typeof to === "string"){
to = to.split('/');
}
console.log('to, data: ',to, data)
if (data) {
this.router.navigate(to, data);
} else {
this.router.navigate(to);
}
});
}
canActivate(route, cb) {
this.oAuth
.authenticate(route.path)
.then((d: any) => {
console.log('d: ', d);
cb(!!d);
})
.catch(e => {
console.log(e, "Error, Chack login no actuive--------------------");
cb(false);
});
}
}
当我使用 activity.goto('xyz/xy1') 或通过浏览器加载 ('/xyz/xy1') 进行路由时,它适用于根路由,但对于子路由,它只是重定向到主路由。