Как решить проблему с subcribe () для моего app.component, если моя консоль показывает мне следующее: ERROR TypeError: Невозможно прочитать свойство 'subscribe' из неопределенного
в AppComponent.push ../ src / app / app.component.ts.AppComponent.ngOnInit
data.service:
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Injectable } from '@angular/core';
import { Observable, Subject, ReplaySubject } from 'rxjs';
import { from, of, range } from 'rxjs/create';
import { map, filter, switchMap } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class DataService {
constructor(private http: Http) { }
fetchData(){
this.http.get('assets/ninjas.json').pipe(
map((res) => return res.json()))}}
app.components:
import { Component } from '@angular/core';
import { DataService } from './data.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app';
ninjas = [];
constructor(private newService: DataService){ }
ngOnInit(){
this.newService.fetchData().subscribe(
(data) => this.ninjas = data;
);
}
}