Angular ngOnInit()属性don';t upate值
我有两个角度方法来检查角色和权限。如果其中一个条件为true,则该值将分配给组件中的ManagePermission布尔属性。服务中有两种方法:Angular ngOnInit()属性don';t upate值,angular,typescript,Angular,Typescript,我有两个角度方法来检查角色和权限。如果其中一个条件为true,则该值将分配给组件中的ManagePermission布尔属性。服务中有两种方法: checkProjectManagePermission(): Observable<boolean> { const currentLoggedUser = this.authService.getCurrentLoggedInUser(); const reqUrl = `${currentLoggedUser.mPor
checkProjectManagePermission(): Observable<boolean> {
const currentLoggedUser = this.authService.getCurrentLoggedInUser();
const reqUrl = `${currentLoggedUser.mPortalWebApi}${CONST.ctrlUrl.projects}/check-project-manage-permission`;
return this.http.get(reqUrl).pipe(
map((response: any) => response),
catchError((error) => _throw(error)),
);
}
checkProjectUserRoles(): Observable<boolean> {
const currentLoggedUser = this.authService.getCurrentLoggedInUser();
const reqUrl = `${currentLoggedUser.mPortalWebApi}${CONST.ctrlUrl.projects}/${this.projectId}/check-project-user-roles`;
return this.http.get(reqUrl).pipe(
map((response: any) => response),
catchError((error) => _throw(error)),
);
}
我在代码中遗漏了什么?提前谢谢 这是一个异步问题。ngInit在checkProjectManagePermission()完成之前完成。在ngOnInit中订阅checkProjectManagePermission() 为确保不会出现内存泄漏,请将checkProjectManagePermission()分配给一个变量,然后在Ngondestory中对其调用unsubscribe。试试这个
projectManagePermissions = false;
ngOnInit() {
this.showLoader = true;
this.checkProjectManagePermission();
}
private checkProjectManagePermission() {
this.projectManagementService.checkProjectManagePermission()
.pipe(takeUntil(this.observablesDispose$))
.subscribe(
(response) => {
this.projectManagePermissions = response;
if (!this.projectManagePermissions) {
this.checkProjectUserRoles();
}
}
)
}
private checkProjectUserRoles() {
return this.projectManagementService.checkProjectUserRoles()
.pipe(takeUntil(this.observablesDispose$))
.subscribe(
(response) => {
this.projectManagePermissions = response
}
)
}
这仍然不起作用1.我提出了你的建议。我注意到另一个奇怪的事情,当我转到另一个链接并返回时,如果我再次重复或刷新页面,这是一个很好的值,它是false@BytechThis可能会有所帮助
projectManagePermissions = false;
ngOnInit() {
this.showLoader = true;
this.checkProjectManagePermission();
}
private checkProjectManagePermission() {
this.projectManagementService.checkProjectManagePermission()
.pipe(takeUntil(this.observablesDispose$))
.subscribe(
(response) => {
this.projectManagePermissions = response;
if (!this.projectManagePermissions) {
this.checkProjectUserRoles();
}
}
)
}
private checkProjectUserRoles() {
return this.projectManagementService.checkProjectUserRoles()
.pipe(takeUntil(this.observablesDispose$))
.subscribe(
(response) => {
this.projectManagePermissions = response
}
)
}