如何刷新路线的已解析数据 [英] How to refresh a route's resolved data
问题描述
请考虑以下路由配置:
const routes: Routes = [
{
path: '',
component: AppComponent,
resolve: {
app: AppResolver
},
children: [
{
path: 'user/:uid',
resolve: {
user: UserResolver
},
children: [
{
path: '',
component: ViewUserComponent
},
{
path: 'edit',
component: EditUserComponent
}
]
}
]
}
];
UserResolver
检索路径/user/:uid
的任何子级的用户数据.
使用此数据,可以通过导航到/user/:uid
来查看用户配置文件,然后通过导航到/user/:uid/edit
来进行编辑.
编辑成功后,可以期望将其重定向到用户的更新配置文件(/user/:uid
).
The user data are retrieved by UserResolver
for any children of route /user/:uid
.
Using this data, a user profile can be viewed by navigating to /user/:uid
and then edited by navigating to /user/:uid/edit
.
Upon a successful edit, one would expect to be redirected to the user's updated profile (/user/:uid
).
但是,视图和编辑页面都是已解析路径的子代,因此从一个导航到另一个导航时不会触发解析器.
However, both the view and edit page being children of the resolved route, the resolver is not triggered when navigating from one to the other.
因此,当从编辑组件导航到视图组件时,显示的数据仍然是旧的,未更新的数据.
Consequently, when navigating back from the edit component to the view component, the data displayed is still the old, non-updated one.
是否有任何方法可以使解析的user
数据无效,以迫使解析器再次从服务器获取数据?还是有其他方法可以达到这种效果?
Would there be any way to invalidate the resolved user
data to force the resolver to fetch it again from the server? Or is there any other way to achieve that kind of result?
app
数据保持不变,我们不必重新加载它.
The app
data being unchanged, we shouldn't have to reload it.
推荐答案
我已经测试了解决方案"runGuardsAndResolvers",它适用于我的用例,在每个子页面更改时都调用该分解器.
I've tested the solution 'runGuardsAndResolvers', it works for my use case, the resolver is called at each sub-page changes.
仅将此选项添加到您希望以这种方式运行的部分路由中.
It is only adding this option the part of your routes that you want behave this way.
export const ROUTES: any = [
{
path: 'project/:name',
runGuardsAndResolvers: "always",
component: ProjectComponent,
resolve: { project: ProjectResolver },
children: [
...
这篇关于如何刷新路线的已解析数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!