I am trying to inject a service to my component and I think I have written everything correctly but it gives me an error saying Constructor implementation is missing
.
My component is:
import { Component, OnInit } from '@angular/core';
import {Lists} from "./lists";
import {ListsService} from "./lists.service";
@Component({
selector: 'app-lists',
templateUrl: './lists.component.html',
styleUrls: ['./lists.component.css']
})
export class ListsComponent implements OnInit {
lists: Lists[] = [];
constructor(private listService: ListsService){}
ngOnInit() {
this.lists = this.listService.getItems();
}
}
My service:
import {Lists} from "./lists";
export class ListsService {
private lists: Lists[] = [];
addLists(list: Lists){
this.lists.push(list);
console.log(`You added ${list}`)
}
getItems(){
return this.lists;
}
}
and my template is:
<app-lists-edit></app-lists-edit>
<app-lists-row *ngFor="let list of lists" [item]="list"></app-lists-row>
Anything that I have done here is a mistake. I even entered the service in providers in ngModule
.
And other errors are:
ERROR in C:/Sites/Angular2main/todolist/src/app/lists/lists.ts (2,15): A parameter property is only allowed in a constructor implementation.
ERROR in C:/Sites/Angular2main/todolist/src/app/lists/lists.ts (3,15): A parameter property is only allowed in a constructor implementation.
ERROR in C:/Sites/Angular2main/todolist/src/app/lists/lists.ts (4,15): A parameter property is only allowed in a constructor implementation.
ERROR in C:/Sites/Angular2main/todolist/src/app/lists/lists.ts (4,15): A parameter initializer is only allowed in a function or constructor implementation.
Well my mistake was in my model
export class Lists {
constructor(private title: string, private description: string, private completed: boolean)
}
I didn't put curly brackets after my constructor.
The error message states that you need a constructor in your service.
Therefore add a contructor() {}
in your ListsService
.
Although the cause of your problem is not clear, you may need to show us the contents of your lists.ts
as has stated PierreDuc in order to understand the cause.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With