2017-02-19 14 views
-1

私は角度2を学習しています。この例では、isLoadingの変数をtrueに設定し、それをfalseに変更します。必要、私はエラーを取得する:Typescript - 型 'true'に代入することはできません

Typescript - type 'false' is not assignable to type 'true'

これはコードです:

export class AppComponent implements OnInit { 
    isLoading: true; 

    constructor(private _articleService: ArticleService, private _postService: PostService){} 

    ngOnInit(){ 
     this.articles = this._articleService.getArticles(); 
     this._postService.getPosts() 
     .subscribe(posts => { 
      this.isLoading = false; 
      console.log(posts[0].title); 
     }); 
    } 
+1

'isLoading:真;' - > 'isLoading =真;' – jonrsharpe

+1

'isLoading:ブール;' –

答えて

2

時に変数の型を定義するために必要な、しかし、あなたは見つけるために設定する必要がありますされません開発中のバグ。さらに、コードを完成させるために知っているエディタで、彼は変数の型を知っています。

export class AppComponent implements OnInit { 
isLoading : boolean = true; 

constructor(private _articleService: ArticleService, private _postService: PostService){} 

ngOnInit(){ 
    this.articles = this._articleService.getArticles(); 
    this._postService.getPosts() 
    .subscribe(posts => { 
     this.isLoading = false; 
     console.log(posts[0].title); 
    }); 
} 
6

isLoading: true;あなたはisLoadingに割り当てることができる唯一の値が値trueであることを意味します。あなたはisLoading: booleanが欲しいです。コメントや@Louisからの回答からの提案に基づき

+0

しかし、私は唯一の ''に設定した場合、データがロードされている場合、私は私のテンプレートでそれを確認する方法'ブール' ''?私はこれを行うには偽りが必要です。 – Leff

+0

@Leff 'isLoading:boolean;'はisLoading:boolean = null;と言っているのと同じです。ヌルは偽であるので、実質的に 'isLoading'をfalseにデフォルト設定しています。あなたのテンプレートは不平を言うべきではありません。 – leff

0

、これはそれを修正する権利構文た:

isLoading: boolean = true; 
関連する問題