As a newcomer to Angular 2.0, I've been delving into new concepts in order to grasp it better. However, despite encountering this common issue multiple times and reading through various solutions, I haven't been able to find the answer to my problem. Specifically, I am struggling with inserting a date object into an interface that consists of certain elements. Even after numerous attempts to enter the date object, I continue to face errors.
Here is a snippet of my code:
export class Lesson {
id: number
name: string
image_url: string
start_time: Date
end_time: Date
}
While trying to input dummy data in my TypeScript file, everything seems to work fine for each element except for 'start_time' and 'end_time', where I consistently encounter the following error: Unexpected token. A constructor, method, accessor, or property was expected.
The relevant portion of my TypeScript file looks like this:
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() {
}
}
Despite attempting various solutions, such as:
var date = '2017-11-27T09:10:00';
Date time = new Date(date);
I received an error like this:
Class 'MyClassesComponent' incorrectly implements interface 'OnInit'. Property 'ngOnInit' is missing in type 'MyClassesComponent'.
I kindly ask for assistance, as I am still learning. Any guidance will be greatly appreciated. Thank you.
Note: The emphasis here is on working within the TypeScript itself at the moment. Hence, I require assistance solely in this area.
To address any concerns regarding potential import-related issues, I have already ensured that all necessary imports are in place. The challenge lies in passing the date object into 'start_time' and 'end_time'. import {Component, OnInIt} from '@angular/core'