Ошибка углового 5 HTTP загрузки json файла - PullRequest
0 голосов
/ 17 мая 2018

newsService.ts

import { Injectable } from '@angular/core';
    import { HttpClient } from '@angular/common/http';
    import { INews } from './inews';
    import { Observable } from 'rxjs';
    import { map } from 'rxjs/operators';

    @Injectable({
      providedIn: 'root'
    })
    export class NewsserviceService {
      public _url = 'http://localhost/admin/demo/api/get_posts/';
      constructor( public http: HttpClient) { }
      getNews(): Observable<INews[]> {
        return this.http.get<INews[]>(this._url).pipe(map(res => res.json()));
      }
     }

newsComponent.ts

import { Component, OnInit } from '@angular/core';
import { NewsserviceService } from './newsservice.service';

@Component({
  selector: 'app-news-ticker',
  templateUrl: './news-ticker.component.html',
  styleUrls: ['./news-ticker.component.scss']
})
export class NewsTickerComponent implements OnInit {
  public news: any[];
  constructor(private newsservice: NewsserviceService) { }

  ngOnInit() {
    this.loadNews();
  }
  loadNews(): void {
    this.newsservice.getNews().subscribe(res => this.news = res);

  }

}

newscomponent.html

<section class="news-ticker">
  <div class="container">
    <div class="row">
      <div class="col-md-12">
        <div class="ticker">
          <strong class="pull-left">news flash</strong>
          <ul>
            <li *ngFor='let news of news'>
              {{news.content}}
            </li>
          </ul>
        </div>
      </div>
    </div>
  </div>
</section>

ошибка консоли

ОШИБКА TypeError: res.json не является функцией на MapSubscriber.project (newsservice.service.ts: 14) в MapSubscriber.push ../ node_modules / rxjs / _esm5 / internal / operator / map.js.MapSubscriber._next (map.js: 75) в MapSubscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.Subscriber.next (Subscriber.js: 93) в MapSubscriber.push ../ node_modules / rxjs / _esm5 / internal / operator / map.js.MapSubscriber._next (map.js: 81) в MapSubscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.Subscriber.next (Subscriber.js: 93) в FilterSubscriber.push ../ node_modules / rxjs / _esm5 / internal / operator / filter.js.FilterSubscriber._next (filter.js: 85) в FilterSubscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.Subscriber.next (Subscriber.js: 93) в MergeMapSubscriber.push. на InnerSubscriber.push ../ node_modules / rxjs / _esm5 / internal / InnerSubscriber.js.InnerSubscriber._next (InnerSubscriber.js: 20) на InnerSubscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.Subscriber.next (Subscriber.js: 93)

1 Ответ

0 голосов
/ 17 мая 2018

лучше, если вы напишете некоторую информацию о вашей проблеме, однако ваша проблема относится к

 getNews(): Observable<INews[]> {
            return this.http.get<INews[]>(this._url).pipe(map(res => res.json()));
      }

в вашей службе, измените ее на:

   getNews():Observable {
        return this.http.get(this._url)
      }

, так как метод дает нам заметныеследует подписать его в целевой компонент, который вы можете просто сделать:

loadNews() {
    this.newsservice.getNews().subscribe(res => this.news = res);

  }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...