vla*_*rin 14 typescript angular2-router angular
请考虑以下路由配置:
const routes: Routes = [
{
path: '',
component: AppComponent,
resolve: {
app: AppResolver
},
children: [
{
path: 'user/:uid',
resolve: {
user: UserResolver
},
children: [
{
path: '',
component: ViewUserComponent
},
{
path: 'edit',
component: EditUserComponent
}
]
}
]
}
];
Run Code Online (Sandbox Code Playgroud)
用户数据由UserResolver路径的任何子节点检索/user/:uid.使用此数据,可以通过导航到导航到/user/:uid然后编辑来查看用户配置文件/user/:uid/edit.成功编辑后,可能会重定向到用户的更新配置文件(/user/:uid).
但是,视图和编辑页面都是已解析路径的子项,当从一个路径导航到另一个路径时,不会触发解析器.
因此,当从编辑组件导航回视图组件时,显示的数据仍然是旧的,未更新的数据.
是否有办法使已解析的user数据无效以强制解析器再次从服务器获取它?或者有没有其他方法来实现这种结果?
该app数据是不变的,我们不应该重新加载它.
slu*_*cas 14
我已经测试了解决方案'runGuardsAndResolvers',它适用于我的用例,在每个子页面更改时调用解析器.
它只是添加此选项,您希望以这种方式表现您的路线部分.
export const ROUTES: any = [
{
path: 'project/:name',
runGuardsAndResolvers: "always",
component: ProjectComponent,
resolve: { project: ProjectResolver },
children: [
...
Run Code Online (Sandbox Code Playgroud)
你可以尝试这样的事情:
constructor(private activatedRoute: ActivatedRoute) {
this.activatedRoute.parent.data.subscribe(data => {
this.profile = data.profile;
});
}
updateProfile(newProfile) {
(this.activatedRoute.parent.data as BehaviorSubject<any>).next({profile: newProfile});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7413 次 |
| 最近记录: |