缺少构造函数实现

时间:2017-07-24 12:33:57

标签: angular typescript

我正在尝试向我的组件注入一个服务,我想我已经正确地编写了所有内容,但它给了我一个错误Constructor Implementation is missing

我的组件是

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();
  }

}

我的服务

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;
  }
}

我的模板是

<app-lists-edit></app-lists-edit>
<app-lists-row *ngFor="let list of lists" [item]="list"></app-lists-row>

我在这里做的任何事都是错误的。我甚至在ngmodule的提供者中输入了服务。

其他错误

错误

RROR 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.

我的错误在于我的模型

export class Lists {
  constructor(private title: string, private description: string, private completed: boolean)
}

我的构造函数之后没有放大括号。

2 个答案:

答案 0 :(得分:9)

错误消息指出您需要服务中的构造函数。

因此,在contructor() {}中添加ListsService

虽然问题的原因尚不清楚,但您可能需要向我们展示lists.ts的内容,正如PierreDuc所说,以了解原因。

答案 1 :(得分:0)

如果您为构造函数的参数之一提供错误的类型,也会出现此类错误。 enter image description here

研究它们并进行修复以消除此错误。 enter image description here