Reputation: 9018
I'm new to Angular 2.0 and have been working on new things to learn it. Since this is very common problem which I have seen, after reading a lot of answers I still did not reach to my solution. My problem is I'm trying to put the date object in the interface which contains certain elements. After trying to enter the date object still I'm getting the error.
MyCode shown below:
export class Lesson {
id: number
name: string
image_url: string
start_time: Date
end_time: Date
}
In my typescript I'm trying to enter the dummy data, for every elements it is fine but when it comes to start_time and end_time it is giving me this error always : Unexpected token. A constructor, method, accessor, or property was expected.
The code goes like this in my typescript file :
export class MyClassesComponent implements OnInit {
var date = '2017-11-27T09:10:00';
var time = new Date(date);
lesson : Lesson = {id: 10, name: "Test Lesson", image_url: "abc", start_time: time, end_time: time}
constructor() { }
ngOnInit() {
}
}
Since I've tried a lot and tried these things also which is not fruitful :
var date = '2017-11-27T09:10:00';
Date time = new Date(date);
for this I got something like this :
Class 'MyClassesComponent' incorrectly implements interface 'OnInit'. Property 'ngOnInit' is missing in type 'MyClassesComponent'.
Please treat me as a learner and help. Since I've tried but don't know how to tackle this. Any help would be highly appreciated. Thanks.
Note: Since this code has to work in the typescript itself I'm working on it right now. So need help in this section only.
Another point for those who might think the problem must be due to the importing stuff. For this I've already done my homework and everything is imported, it is just that I have to pass the date object in the start_time and end_time. import {Component, OnInIt} from '@angular/core'
Upvotes: 0
Views: 14583
Reputation: 126
First fo all you shouldn't use var
inside of a class declaration in TypeScript
. You are only declaring which field can be populated, setting attributes on an instance.
Second of all you shouldn't use var
at all, use let
or const
instead.
A more proper code should be:
export class MyClassesComponent implements OnInit {
date = '2017-11-27T09:10:00';
time = new Date(date);
lesson : Lesson = {id: 10, name: "Test Lesson", image_url: "abc", start_time: time, end_time: time}
constructor() { }
ngOnInit() { }
}
See if that fixes your problem because your error says you implement OnInit incorrectly (onInit
is a kind of pseudo-constructor), and it seems to be implemented ok.
I think it would help you a lot if you finished some TypeScript
introduction tutorial first, I know I was confused by Angular
a lot in the beginning, because I didn't know if the think I was learning was Angular
specific or just ES6 syntax.
Upvotes: 3