Angular 4:注入Service后组件消失

时间:2018-03-03 17:20:49

标签: angular angular-services angular-components

我在Components中遇到ServicesAngular 4的问题。 所以我得到了CustomerComponent

@Component({
selector: 'app-customer',
templateUrl: './customer.component.html',
styleUrls: ['./customer.component.css']
})
export class CustomerComponent implements OnInit {

customers: Customer[] = [];
constructor() { }

Customer Model

export class Customer {
constructor(
public id: number,
public firstName: string,
public lastName: string
) {  }
}

CustomerService

@Injectable()
export class CustomerService {

private customersUrl = 'http://localhost:9090/customer';
constructor(private http: Http) { }

// Get all customers
getCustomers(): Promise<Customer[]> {
return this.http.get(this.customersUrl)
  .toPromise()
  .then(response => response.json() as Customer[])
  .catch(this.handleError);
}

现在我想在AppComponent中显示CustomerComponent。 通过添加<app-customer></app-customer>可以显示:customer works

但是当我将CustomerService添加到CustomerComponent的构造函数中时,这样:

 constructor(private customerService: CustomerService) { }

customer works不再显示,我在控制台中没有收到错误消息。所以我不知道问题是什么。有人可以帮忙吗?感谢

1 个答案:

答案 0 :(得分:0)

我发现了问题所在。我忘记导入HttpModule中的AppModule。但我仍然不知道为什么我在任何地方都没有看到错误...