簡體   English   中英

Angular ngOnInit() 屬性不會更新值

[英]Angular ngOnInit() property don't upate value

我有兩個角度方法來檢查角色和權限。如果這些條件之一為真,則該值分配給組件中的 managePermsission 布爾屬性。 這是服務中的兩種方法:

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)),
    );
}

這是組件中的代碼。 如果此方法中的某些方法返回 true,我想將屬性 projectManagePermssions 設置為 true,但第二種方法始終將 projectManagePermssions 設置為 false,盡管服務響應返回 true 值。

projectManagePermissions = false;

 ngOnInit() {       
    this.showLoader = true;
   
    this.checkProjectManagePermission();       
  
   if (!this.projectManagePermissions)         
      this.checkProjectUserRoles();                                 
}

private checkProjectManagePermission() {
    this.projectManagementService.checkProjectManagePermission()
        .pipe(takeUntil(this.observablesDispose$))
        .subscribe(
            (response) => {              
                this.projectManagePermissions = response                    
            }
        )
}

private checkProjectUserRoles() {
    return this.projectManagementService.checkProjectUserRoles()
        .pipe(takeUntil(this.observablesDispose$))
        .subscribe(
            (response) => {                  
                this.projectManagePermissions = response                
            }
        )
}

我在代碼中遺漏了什么? 提前致謝 !

嘗試這個。

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                
                }
            )
    }

這是一個異步問題。 ngInit 在 checkProjectManagePermission() 之前完成。 在 ngOnInit 中訂閱 checkProjectManagePermission()。

為確保您不會出現內存泄漏,請將 checkProjectManagePermission() 分配給一個變量,然后在 ngOnDestroy 中調用取消訂閱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM