首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在类Typescirpt/angular2 2中使用const关键字

如何在类Typescirpt/angular2 2中使用const关键字
EN

Stack Overflow用户
提问于 2016-11-03 05:02:26
回答 1查看 2.2K关注 0票数 1

我正试图在我的课堂上使用const maxOverviewTextLength:number = 20;。但是我的ide vscode给出了错误A class member cannot have the const keyword

我怎样才能在课堂上使用const。请帮帮忙。

代码语言:javascript
复制
export class HomeComponent implements AfterViewInit{

movieName:string = "";

movieSearchActive:boolean = true;

popularMovieData:Array<any> = [];

const maxOverviewTextLength:number = 20;

constructor(private _movieListService:MovielistsService){

}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-03 05:08:00

您不能使用const,因为在JS中所有类实例都是可变的。但是,您可以使用readonly来让TypeScript提示错误。

关于readonlyhttps://basarat.gitbook.io/typescript/type-system/readonly的更多信息

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40394163

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档