错误:ReferenceError:赋值中的左侧无效

时间:2016-05-27 18:45:41

标签: typescript angular

我知道JavaScript。我只是学习Typescript,这给了我错误:“错误: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

中的app/app.component.ts文件

1 个答案:

答案 0 :(得分:3)

在类体内,您可以拥有初始化成员。例如,您声明了things类型的string[]。这是合法的成员声明。

不能在你的班级团体中有陈述。代码things[1] = "c";是一个声明。如果你想做那样的事情,你应该把它放在constructor