Hello there,
I am trying to retrieve API data with a token using Angular 2/4. Below is the code I have written:
import { Component, ViewEncapsulation } from '@angular/core';
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/map';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
encapsulation: ViewEncapsulation.None,
})
export class AppComponent {
private apiUrl = 'http://apiurlhere.xom/data';
data: any = {};
constructor(private http: Http){
console.log('hi');
this.getVoicepickData();
this.getData();
}
//set API header
let headers = new Headers({
'Token': "XXXXXXXXXXTOKEN HEREXXXXXXXXXX",
'Content-Type': 'application/json'
});
getData(){
return this.http.get(this.apiUrl, {headers: headers})
.map((res: Response) => res.json())
}
getVoicepickData() {
this.getData().subscribe(data => {
console.log(data);
this.data = data
})
}
}
I encountered an error stating: Module parse failed: 'return' outside of function. Can someone guide me on how to successfully retrieve the API data with a token? Thank you for your assistance.