App Module
import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { SplashScreen } from '@ionic-native/splash-screen';
import { StatusBar } from '@ionic-native/status-bar';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import { HttpClientModule } from '@angular/common/http';
@NgModule({
declarations: [
MyApp,
HomePage
],
imports: [
BrowserModule,
HttpClientModule,
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
HomePage
],
providers: [
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
})
export class AppModule {}
home.ts
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Car } from '../../models/car'; /* importando interface TS */
import { HttpClient } from '@angular/common/http'; /* protocolo */
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
public cars: Car[];
constructor(public navCtrl: NavController, private _http: HttpClient) {
this._http.get<Car[]>('http://localhost:8080/api/carro/listaTodos')
.subscribe((cars) => {
this.cars = cars;
}
);
}
}
home.htm
<ion-header>
<ion-navbar>
<ion-title>
App Ionic
</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<ion-list>
<ion-item *ngFor="let car of cars">
<h3>{{car.name}}</h3>
<p>R${{car.price}}</p>
</ion-item>
</ion-list>
</ion-content>
Me mostra um erro:
run time error object prototype may only be an objetc or null: undefined