gpt4 book ai didi

Angular 路由不刷新页面

转载 作者:行者123 更新时间:2023-12-05 01:06:34 24 4
gpt4 key购买 nike

我希望能够根据 URL 路由更改页面上的数据,即 test/1 获取我的数据集 1,test/2 获取我的数据集 2。但是,如果我已经在 test/1 上并尝试要使用路由器导航导航到 test/2(反之亦然),它会更改 url 但仅此而已,不会触发其他任何内容。

我的路线有以下定义:

const routes: Routes = [
{
path: 'test',
children: [
{
path: ':id',
component: TestComponent
},
{ path: '**', redirectTo: '', pathMatch: 'full' }
]
},
];

TS 组件:

value: any;

constructor(private router: Router, private, private route: ActivatedRoute) {
this.value = this.route.snapshot.params.id;
}

goToPage(value: any) {
this.router.navigate(['/test', value]);
}

我的 html 组件:

{{value}}

<button (click)="goToPage(1)">One</button>
<button (click)="goToPage(2)">Two</button>

我也尝试将 { onSameUrlNavigation: 'reload' } 添加到 RouterModule.forRoot,但仍然没有做任何事情。

注意:问题不在于获取参数,问题在于必须刷新页面才能触发与新参数关联的所有进程。

最佳答案

通过实现以下代码解决了问题:

    this.router.navigate(['/test', value])
.then(() => {
window.location.reload();
});

关于 Angular 路由不刷新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68753614/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com