我的Angular应用程序中有以下Angular结构:
path: ':piva/:negozio',
component: NegozioComponent,
children: [
{
path: '',
component: ModuliComponent,
},
{
path: 'location',
component: LocationComponent
},
{
path: ':type',
component: ProductsComponent,
//I can access this only if 'location' is true
},
{
path: 'item/:id',
component: ItemComponent,
},
];因此,我必须访问路径:type,只有在location中.get请求返回true时,在“位置”页面中有一个输入框,该输入框检查客户端的位置是否在送货商店的范围内,如果在该范围内,则必须将用户发送到:type。
因此,我试图在我的location.component中提交以下内容:
getLocation(): void {
this.locationService
.inRange(
this.addressComponent.geometry.location.lat(),
this.addressComponent.geometry.location.lng()
)
.subscribe((data: any) => {
if (data.inRange) {
this.router.navigate([
'asporto',
{ relativeTo: this.activatedRoute },
]);
}
});
}但是,当我尝试提交button时,控制台中会出现以下错误:
Error: Cannot match any routes. URL Segment: 'asporto; relativeTo = Route%28url: 'location', %20path:' location'%29'发布于 2020-09-07 08:20:06
对于这个问题,您应该在您的CanActivate路径上使用Guard,并在其中使用locationService。所以做这样的事:
app.route.ts
path: ':piva/:negozio',
component: NegozioComponent,
children: [
{
path: '',
component: ModuliComponent,
},
{
path: 'location',
component: LocationComponent
},
{
path: ':type',
component: ProductsComponent,
canActivate: [locationGuard]
},
{
path: 'item/:id',
component: ItemComponent,
},
];location-guard.service.ts
import { Injectable } from '@angular/core';
import { Router, CanActivate } from '@angular/router';
import { LocationService } from './location.service';
@Injectable()
export class LocationGuard implements CanActivate {
constructor(public locationService: LocationService, public router: Router) {}
canActivate(): boolean {
this.locationService.inRange(
this.addressComponent.geometry.location.lat(),
this.addressComponent.geometry.location.lng()
.subscribe((data: any) => {
if (data.inRange) {
this.router.navigate([`/type`]);
return ture;
}
return false;
});
}
}https://stackoverflow.com/questions/63772598
复制相似问题