Angular 8 - не могу вызвать webapi - PullRequest
       10

Angular 8 - не могу вызвать webapi

0 голосов
/ 27 сентября 2019

Я пытаюсь получить данные из веб-API, используя параметр. Я не могу получить data.no также не отображается сообщение об ошибке.

компонент панели приборов

Countrymodel: Country = {country_id: '', название страны: '', статус: false} Страны: наблюдаемые;

constructor(private api: RestApiService, private router: Router) { }



ngOnInit() {
    let currentUserName = localStorage.getItem('currentUserName');
    console.log(currentUserName);
    this.GetCountry(currentUserName);

}
GetCountry(currentUserName: string) {
    this.Countries = this.api.getcountry(currentUserName);
    console.log(this.Countries);
}

}

api.service

   export class RestApiService {
Url = 'http://localhost:xxxx/Api/Example/';
header: any;
constructor(private http: HttpClient) {
    const headerSettings: { [name: string]: string | string[]; } = {};
    this.header = new HttpHeaders(headerSettings);
}
getcountry(currentUserName: string): Observable<Country[]> {
    console.log(this.Url + 'GetCountry?Username=' + currentUserName);
    return this.http.get<Country[]>(this.Url + 'GetCountry?Username=' + currentUserName);

}

WebApi

[Route("Api/Example/GetCountry")]
    [HttpGet]
    public IHttpActionResult GetCountry(string Username)
    {
    }

1 Ответ

2 голосов
/ 27 сентября 2019

Вы должны подписаться на звонок, если хотите, чтобы он выполнялся.См. HttpClient

this.api.getcountry(currentUserName).subscribe((data)=>{
    console.log(this.Countries);
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...