Reputation: 141
Angular says to me this error when i try tu use one interface and i dont know why.
file: '*******/movies.component.ts'
severity: 'Error'
message: 'Type '{ title: string; id: string; }' is not assignable to type
'Movie'.
Property 'consructor' is missing in type '{ title: string; id: string; }'.'
at: '16,9'
source: 'ts'
i have this interface:
export class Movie{
title : string;
id : string;
consructor(title : string, id: string){
this.title = title;
this.id = id;
}
}
And i have this class
export class MoviesComponent implements OnInit {
movies:Movie;
constructor(){
}
ngOnInit() {
this.movies = {title: 'ToyStory', id:'134'};
}
}
Upvotes: 1
Views: 499
Reputation: 109
try this below:
in movie.model.ts:
export interface IMovie {
title: string;
id: string;
}
export class Movie implements IMovie {
constructor(
public title: string,
public id: string
) { }
}
in movie.component.ts:
export class MoviesComponent implements OnInit {
private _movies: Movie[];
ngOnInit() {
this._movies.push(new Movie('ToyStory','134'));
}
}
Why are you using a string for id?
Upvotes: 0
Reputation: 12036
Typo should be constructor(title : string, id: string)
what syntax is this? This is wrong
this.movies = {title: 'ToyStory', id:'134'}
This is how you create an object
this.movies=new Movie('ToyStory','134');
Upvotes: 1