2016-04-20 14 views
1

我在我的項目中使用TypeScript,我無法將屬性分配給任何東西。我試圖從服務中獲取一些數據並將其分配給使用構造函數聲明的私有對象。但是,我不斷收到相同的錯誤:TypeError:無法在這裏[某些數據]上創建屬性[某些屬性]。 這裏是我的代碼:Typescript:不能在函數內創建屬性

module MyModule.Controllers { 
    "use strict"; 

    export interface ICurrentUser { 
     DisplayName: string; 
     Features: string[]; 
    } 

    export class DashboardController { 
     authenticationService = new Services.AuthenticationService(this.$http); 
     static $inject = ["$http"]; 

     constructor(private $http: ng.IHttpService, private currentUser: ICurrentUser, private currentUserFeatures: string[]) 
     { 
      this.getCurrentUser(); 
      this.getUserGroupForCurrentUser(); 
      return; 
     } 

     getUserGroupForCurrentUser =() => { 
      this.authenticationService.getUserGroupForCurrentUser().then((authenticationId) => { 
       this.currentUser.Features = authenticationFeatures; 
      }); 
     } 

     getCurrentUser =() => { 
      this.authenticationService.getCurrentUser().then((user) => { 
       this.currentUser.DisplayName = userName; 
      }); 
     } 

    } 

} 
+0

需要得到的實際錯誤 –

+0

TypeError:無法在字符串'John Doe'上創建屬性'DisplayName' – user1789573

+2

我無法弄清楚。看起來像'this.currentUser.DisplayName = userName;'雖然是問題的根源。也許'this.currentUser'不是'ICurrentUser'對象,是別的,因爲它在承諾中? –

回答

1

Cannot create property 'DisplayName' on string 'John Doe' –

這是下面的代碼:

this.currentUser.DisplayName = userName; 

似乎在運行時this.currentUser實際上是一個字符串John Doe。這表明,類型聲明(它們對編譯器提示有關不受其控制的外部系統的有效提示)與運行時的實際外部系統不匹配。

相關問題