为什么类成员不能在TypeScript中使用'const'关键字?
我在TypeScript documentation website找不到任何有用的信息。
答案 0 :(得分:6)
为什么类成员不能在TypeScript中使用'const'关键字?
const
并不意味着深层不变,因此以下内容有效:
const foo:any = {};
foo.bar = 123; // Okay
从这个意义上说, readonly 对于班级成员更有意义,并得到支持:https://basarat.gitbooks.io/typescript/content/docs/types/readonly.html
答案 1 :(得分:2)
好吧,我的朋友,从我认识的那个小小以及我在这里做的测试,下面的代码都可行。 " const ..."需要在课外,如下所示:
import { Component, OnInit } from '@angular/core';
const HEROES: any[] = [
{id: 1, name: "Rafael Moura"},
{id: 2, name: "Hulk"},
{id: 3, name: "Iron Man"},
{id: 4, name: "Spider-Man"},
{id: 5, name: "Super-Man"},
{id: 6, name: "Thor"},
{id: 7, name: "Wolverine"},
{id: 8, name: "Cyclop"},
{id: 9, name: "Magneto"},
{id: 10, name: "Arrow"},
{id: 11, name: "Geen"}
]
@Component({
selector: 'app-component-heroes',
templateUrl: './component-heroes.component.html',
styleUrls: ['./component-heroes.component.css']
})
export class ComponentHeroesComponent implements OnInit {
title = "Tour of Heros";
heroes = HEROES;
constructor() {
}
ngOnInit() {
}
}