Search code examples
angularangular-servicesangular-components

Angular 4: Component disappears after injecting Service


I got a problem regarding Components and Services in Angular 4. So I got the CustomerComponent:

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

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

And the Customer Model:

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

And the 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);
}

Right now i want to display the CustomerComponent in the AppComponent. Which works by adding <app-customer></app-customer> it displays: customer works.

But when I'm adding the CustomerService into the Constructor of the CustomerComponent like this:

 constructor(private customerService: CustomerService) { }

The customer works doesnt get displayed anymore and i don't get an error message in the console. So I don't know what the problem is. Could somebody help here? Thanks


Solution

  • I found out what the problem was. I forgot to import the HttpModule in the AppModule. But i still don't know why i didn't see an error anywhere...