admin管理员组文章数量:1401199
I am trying to build a movie catalog using Angular and ionic. The movie class contains id, title, image and plot.
In the first page, the app displays only id title & image. It omits plot.
Movie class
export class Movie{
id: number;
title: string;
img: string;
plot?: string;
}
In the main typescript file, I am assigning the movie details as follows
export class MainPage implements OnInit{
movies: Movie[];
getMovies(){
this.movies = [{id: 1, title: 'test', img: 'img1'}]
}
ngOnInit(){
this.getMovies();
}
}
But this returns following error
Error TS1112: A class member cannot be declared optional. It is referring to the 'plot' variable.
How can I achieve what I intended to? Is there any other options?
Thanks in advance for your help!
I am trying to build a movie catalog using Angular and ionic. The movie class contains id, title, image and plot.
In the first page, the app displays only id title & image. It omits plot.
Movie class
export class Movie{
id: number;
title: string;
img: string;
plot?: string;
}
In the main typescript file, I am assigning the movie details as follows
export class MainPage implements OnInit{
movies: Movie[];
getMovies(){
this.movies = [{id: 1, title: 'test', img: 'img1'}]
}
ngOnInit(){
this.getMovies();
}
}
But this returns following error
Error TS1112: A class member cannot be declared optional. It is referring to the 'plot' variable.
How can I achieve what I intended to? Is there any other options?
Thanks in advance for your help!
Share Improve this question asked Jul 22, 2016 at 10:50 Vyas RaoVyas Rao 4986 gold badges10 silver badges25 bronze badges1 Answer
Reset to default 4If Movie is just defining a data structure and doesn't contain any implementation code, then just change it to an Interface:
interface Movie{
id: number;
title: string;
img: string;
plot?: string;
}
Alternatively the class can just define plot without the ? However in this case you will need to set plot to null in your getMoves method:
class Movie{
id: number;
title: string;
img: string;
plot: string;
}
class MainPage {
movies: Movie[];
getMovies(){
this.movies = [{id: 1, title: 'test', img: 'img1', plot: null}]
}
ngOnInit(){
this.getMovies();
}
}
本文标签: javascriptError TS1112 A class member cannot be declared optionalStack Overflow
版权声明:本文标题:javascript - Error TS1112: A class member cannot be declared optional - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744263595a2597837.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论