2016-05-27 7 views
-1

JavaScriptが分かります。私はTypescriptを学んでいるだけで、これは私にエラーを与えます: "Error:ReferenceError:代入の左手が無効です"。どうしましたか?Error:ReferenceError:代入の左辺が無効

import { Component } from '@angular/core'; 
import { Fruit } from './fruit'; 
import { PartComponent } from './part.component'; 
import { ByKindPipe } from './by-kind.pipe'; 

@Component({ 
    selector: 'my-app', 
    templateUrl: 'app/app.html', 
    directives:[PartComponent], 
    pipes: [ByKindPipe], 
    styles: ['div { color: blue; }'] 
}) 
export class AppComponent { 

    // this works 
    fruits: Fruit[] = [ 
      {"name": "apple", "kind": "tree"}, 
      {"name": "orange", "kind":"tree"}, 
      {"name": "strawberry", "kind": "berry"}, 
      {"name": "pear", "kind": "tree"}]; 

    // this works  
    things: string[] = ["a","b"]; 

    // this doesn't work 
    things[1] = "c"; 
} 

クラス本体の内部でこのplunk

+0

あなたは何が起こっているのかを理解するためにここに完全な文脈を含める必要があります。 –

+0

それは塊の中にあります –

+3

あなたは完全な文脈** here **を含める必要があります。 –

答えて

3

app/app.component.tsファイルを参照してください、あなたがメンバーを初期化していることができます。たとえば、string[]thingsを宣言したとします。それは合法的なメンバー宣言です。

にはあなたのクラスのボディにステートメントがありません。コードthings[1] = "c";は声明です。そのようなことをしたい場合は、それをconstructorに入れてください。

+0

普通の古いjavascriptオブジェクトで同じことをした場合、これが引き起こすエラーを考えてみましょう。 –

関連する問題