Angular Can';t在routerLink中导航传递参数
我面临一个奇怪的问题:使用routerLink,我可以在任何地方导航,但如果我在当前路线中添加另一个route param,它就不起作用了 这是我的navigator.component。我使用以下代码将URL传递到我的应用程序卡组件Angular Can';t在routerLink中导航传递参数,angular,typescript,angular6,Angular,Typescript,Angular6,我面临一个奇怪的问题:使用routerLink,我可以在任何地方导航,但如果我在当前路线中添加另一个route param,它就不起作用了 这是我的navigator.component。我使用以下代码将URL传递到我的应用程序卡组件 <div class="installations-grid" *ngIf="installations != null"> <div class="no-cards" *ngIf="installations.length == 0">
<div class="installations-grid" *ngIf="installations != null">
<div class="no-cards" *ngIf="installations.length == 0"><p i18n="no_installations| no installations string">Nessuna installazione per questo progetto</p></div>
<div class="box-cards" *ngFor="let installation of installations">
<app-card [title]="installation.name" [image-url]="installation.url" [content-text]="installation.id" [url]="'/dashboard/map/' + project.id + '/' + installation.id"></app-card>
</div>
任何其他路径(如/login)都能正常工作。。。
我认为一些因素可能导致这种行为:
注意:它到达Navigator Resolver,但地址栏中的URL和显示的组件模板保持不变 构建路径参数数组并将其传递给routerLink指令。 例如:
modelUrl:Array=['dashboard'、'map'、'projectId'、'installationId']
并将其分配给routerLink
...
其中modelUrl是路径参数的数组
注意:我在lazyloaded模块中遇到了类似的问题,当我将一个数组传递给routerLink指令时,该模块工作正常。当您使用routerLink指令作为输入时(使用方括号,[routerLink]=“arg”),它需要一个字符串数组作为参数
<mat-card class="card-component" [routerLink]="['dashboard', 'map', project.id, installation.id]">...</mat-card>
解决了!在我的导航解决程序类中有一个bug 我在这里调用这个函数:
getInstallations(projectID: string, ignoreCache = false): Observable<any> {
if (!ignoreCache && this.cache[projectID] != null) {
this.logger.debug("cache hit", this.cache[projectID]);
return Observable.create(observer => {
observer.next(this.cache[projectID]);
observer.complete();
});
}
else {
let apiURL = `${Services.GET_INSTALLATIONS}/?project__id=${projectID}`;
return this.apiService.get(apiURL).pipe(map(json => {
let installations = json['objects'];
this.cache[projectID] = installations;
return installations;
}));
}
}
代替:
return Observable.create(observer => {
observer.next(this.cache[projectID]);
observer.complete();
});
不,即使传递一个参数数组,问题仍然存在…:(请尝试在仪表板路由模块中重新排序您的路由。首先是“map”,然后是“map/:projectId”,最后是“map/:projectId/:installationId”[{path:'map',…},{path:'map/:projectId',…},{path:'map/:projectId/:installationId',…}]已经尝试过..奇怪的是,从'map'传递到'map/:projectId'是有效的;它不只是在从'map/:projectId'传递到'map/:projectId/:installationId'或从'map/:projectId/:installationId'传递到'map/:projectId/:installationId/:mapId'时导航,而是尝试使用子路由:[{'map',children children:[path:':projectId',children:[{path:'},{path:':installationId'}]]]路由的顺序很重要。我认为您将通过子路由解决该问题。您可以在此处阅读有关嵌套路由的更多信息。您的问题可能与此相同:
<mat-card class="card-component" routerLink="{{ model.url }}">...<mat-card>
model.url = '/dashboard/map/' + project.id + '/' + installation.id;
getInstallations(projectID: string, ignoreCache = false): Observable<any> {
if (!ignoreCache && this.cache[projectID] != null) {
this.logger.debug("cache hit", this.cache[projectID]);
return Observable.create(observer => {
observer.next(this.cache[projectID]);
observer.complete();
});
}
else {
let apiURL = `${Services.GET_INSTALLATIONS}/?project__id=${projectID}`;
return this.apiService.get(apiURL).pipe(map(json => {
let installations = json['objects'];
this.cache[projectID] = installations;
return installations;
}));
}
}
return of(this.cache[projectID]);
return Observable.create(observer => {
observer.next(this.cache[projectID]);
observer.complete();
});