gpt4 book ai didi

javascript - 延迟加载模块的 Angular onSameUrlNavigation

转载 作者:太空狗 更新时间:2023-10-29 19:31:32 25 4
gpt4 key购买 nike

我在重新获取延迟加载模块数据时遇到问题。onSameUrlNavigation 根本不起作用

我有一个用于延迟加载功能的单独路由模块,也许我无法正确集成 onSameUrlNavigation

项目结构:

app/
app.component.ts
app.module.ts
app-routing.module.ts
someFeature/
someFeature.module.ts
someFeature-routes.module.ts
someFeature.component.ts

应用路由模块代码:

    export const routes: Routes = [{
{
path: 'myfeature',
loadChildren: () => import('./someFeature/someFeature.module').then(m => m.someFeatureModule),
canActivate: [AuthGuard],
runGuardsAndResolvers: 'always',
}}]

@NgModule({
imports: [RouterModule.forRoot(routes, {onSameUrlNavigation: 'reload'})],
exports: [RouterModule],
})
export class AppRoutingModule {
}

懒加载路由模块:

    const routes: Routes = [
{
path: '',
component: someFeatureComponent,
},
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
})

组件代码:

    ngOnInit(): void {
this.unsubscribe$ = new Subject<void>();
this.route
.queryParams.pipe(
takeUntil(this.unsubscribe$),
map(p => {
this.loading = true;
if (p.page && p.limit) {
this.page = p.page;
this.limit = p.limit;
}
return {
page: this.page,
limit: this.limit,
};
}),
flatMap((f) => this.myService.getData(f)),
tap(() => this.loading = false),
)
.subscribe((payload) => {
this.data = payload.data;
})
;
}

这是有问题的部分(在组件代码中)

 reload(): void {
this.router.navigate(['someFeature'], {queryParams: {page: this.page, limit: this.limit}});
}
refetchDataOnClick(){
this.reload();
}

数据是在第一次请求时获取的(所以我认为模块导入或类似的东西没有问题),但没有发生重新获取,因为我可以看到 API 上没有其他请求。

我尝试添加和删除“runGuardsAndResolvers”、someFeature-routes 模块,但没有成功。我还尝试将 onSameUrlNavigation 添加到 someFeature-routes 模块,但是 forChild 只能接受一个参数

最佳答案

感谢用户frido的评论我找到了解决方案

我没有重新加载组件,而是调用再次加载数据的服务

this.loading = true;
this.myService.getData({page: this.page, limit: this.limit})
.pipe(
takeUntil(this.unsubscribe$),
finalize(() => this.loading = false))
.subscribe((payload) => {
this.data = payload.data;
});

关于javascript - 延迟加载模块的 Angular onSameUrlNavigation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57890381/

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