Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructor implementation is missing

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.

like image 300
Mannish Avatar asked Jul 24 '17 12:07

Mannish


1 Answers

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.

like image 88
Christopher Avatar answered Sep 27 '22 19:09

Christopher