【发布时间】:2017-01-16 22:29:25
【问题描述】:
当我使用新参数路由到组件时,我遇到了一个问题,即我的组件正在重新初始化。这是我的路线。
const appRoutes: Routes = [
{ path: '', component: MyNewComponentComponent },
{ path: 'tiles', component: DisplayTileData },
{ path: 'tiles/:master/:filters', component: DisplayTileData }
];
我路由到“图块”并进行服务调用以获取一些数据。然后,我有几个按钮可以返回到具有“master”和“filters”值的相同组件。使用参数路由回组件会重新初始化组件并重复服务调用。我在页面上也有一个文本输入。当我第一次路由到该组件并添加文本时,带有参数的路由也会清除该文本。
<a *ngFor="let tile of tiles">{{tile.id}}</a><br/><br/>
<input class="form-control" maxlength="30" minlength="3" name="from" ng-reflect-maxlength="30">
<button (click)="addNumberToFilter(15)"></button>
<button (click)="addNewMasterPath('do')">add new Number to Filter</button>
有没有办法在使用新参数进行路由时防止此路由重新初始化。
我有按钮的默认值。方法如下。
public variable: any = [3,4];
public master: any = 'eat';
addNewMasterPath(newMasterVariable) {
this.master = this.master + '-' + newMasterVariable;
var newMap = this.variable.map(items => { return items}).join('-');
this.router.navigate(['tiles/', this.master, newMap]);
}
addNumberToFilter(newParameter) {
this.variable.push(newParameter);
var newMap = this.variable.map(items => { return items}).join('-');
this.router.navigate(['tiles/', this.master, newMap]);
}
【问题讨论】:
-
你能发布做实际设置和导航的方法吗?从模板中可以看出,这两个值可以单独设置,所以只是好奇你是如何处理它的。
-
嗯...对。 this.router.navigate(['tiles/', this.master, newMap]);曾经工作。但我面临同样的问题。与
this.router.navigateByUrl('/team/33/user/11');一起工作 -
navigateByUrl 具有相同的行为。