我必须创建一个上一个组件和下一个组件才能使用Angular的路由器转到下一页。问题不在于角度,而在于递增和递减逻辑。
当我单击下一次时,第一次是可以的,但是第二次我必须单击两次才能看到下一页。上一个按钮也发生了同样的情况。如果我想点击返回,在我点击第一个下一个之后,我必须点击两次才能转到上一页。
我看了又看,没看出虫子在哪里。
网页文件
<button class="btn btn-primary" (click)="prevPage()">Prev</button>
<button class="btn btn-primary" (click)="nextPage()">Next</button>
TS 文件
pages = [
{name: '../page1', index: 0},
{name: '../page2', index: 1},
{name: '../page3', index: 2}
];
current = this.pages[0];
getIndex(currentIndex: number, shift: number){
const lenght = this.pages.length;
const incre = (((currentIndex + shift) + lenght) % lenght);
console.log(incre);
return incre;
}
prevPage() {
const i = this.getIndex(this.current.index, -1);
this.current = this.pages[i];
this.router.navigate([this.current.name], { relativeTo: this.route });
console.log(this.current.name);
}
nextPage() {
const i = this.getIndex(this.current.index, 1);
this.current = this.pages[i];
this.router.navigate([this.current.name], { relativeTo: this.route });
console.log(this.current.name);
}
在 HTML 文件中。
<button class="btn btn-primary" (click)="prevPage()">Prev</button>
<button class="btn btn-primary" (click)="nextPage()">Next</button>
将 (单击( 侦听器更改为 nextPage((。
另外,( + 长度(在
常量增量 = ((((当前索引 + 移位( + 长度( % 长度(;
没有它它会正常工作!
常量增量 = ((当前索引 + 移位( % 长度(;