0
Take a good look at the image below;
In my class of services it was like this;
pesquisar(): Promise<any> {
return this._http.get(`${this.url}/menus` )
.toPromise()
.then(response => {
response.json();
})
}
I just need to know what my method will look like in the component class;
I tried like this but I didn’t succeed, I need help;
menu = [];
public title: string;
constructor(
private _menuService: MenuAdminService
) {
this.title = 'Lista de Cardápios';
}
ngOnInit() {
this.pesquisar()
}
pesquisar(){
this._menuService.pesquisar()
.then(menu => {
this.menu = menu;
console.log(this.menu);
});
}
this. _menuService.search() . then(menu => { this.menu = menu.data; console.log(this.menu); });
– Lucas Brogni
@Lucasbrogni gave this error message Error: Uncaught (in Promise): Typeerror: Cannot read Property 'data' of Undefined
– wladyband
take the service then.
– Lucas Brogni
@Lucasbrogni if I take out the service then the method gets error at code build time .
– wladyband
Better use observables at angular
– Eduardo Vargas
@Eduardovargas because it would be better to use observables in that implementation?
– wladyband
@wladyband is the default used. Instead of returning to Promise you rereave an observable and give a subscribe on it.
– Lucas Brogni
We cannot say that Observable is a @Lucasbrogni standard, both are used in the labor market, it will depend on the implementation, in my view it does not need observable, read this => http://www.guj.com.br/t/para-a-bestapproachprojects-angular-observable-or-Promise/365774/9
– wladyband
In this case you would have to use the observable map.
– Eduardo Vargas