【发布时间】:2019-06-10 09:25:26
【问题描述】:
如果详细信息为空,我将尝试调用配置文件 api,以避免每次用户回到家时每次路由更改时都调用 api在页面显示之前。
home 组件中的 console.log 为 null,而 auth 服务中的 console.log 打印正确的配置文件数据。
export class HomeComponent {
activeRoute.data.subscribe((data: any)=> {
this.userDetail = data.profileDetails;
console.log(data);
});
}
应用路由
path: '', component: HomeComponent, resolve: { profileDetails: ProfileDetailsResolverService }
认证服务
getUserProfile() {
if(!this.detail) {
this.http.get(this.userProfileApi).subscribe((res: any)=>{
console.log(res);
this.detail = res.data;
return this.detail;
} else {
return this.detail;
}
});
}
配置文件解析器服务
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if(localStorage.getItem('access_token')){
return this.auth.getUserProfile();
}
}
【问题讨论】:
-
您的解析器中是否定义了
localStorage.getItem('access_token')? -
它只是检查令牌是否存在以及用户登录时令牌是否存在。
-
是的,但它返回
undefined还是一个值?如果您将if注释掉会怎样? -
你可以检查这个方法
getUserProfile()吗?订阅中有一个 else 部分,没有 if 部分..
标签: angular