无法使用 Angular 服务从 Angular 应用程序中的 JSON 文件访问数据

Unable to access data from JSON file in Angular application using Angular services

这是我的 JSON 文件。

{mood: [ {

    "id":"1",
    "text": "Annoyed",
    "cols": 1, 
    "rows": 2, 
    "color": "lightgreen",
    "route":"/angry",

    "musics": [
      {
          "id": "0",
          "name": "English- Heaven's Peace",
          "image": "images/music.png",
          "link": "https://www.youtube.com/playlist?list=PLPfXrbtn3EgleopO8DiEdsNKgqYZZSEKF",
          "descpription": "Tunes that soothe your pained soul",
          "reviews": [
              {                   
                   "name": "abc",
                   "rating": 4,
                   "review": "energetic",
                   "date": ""
              }
          ]
      },
      {
           "id": "1",
           "name": "English- Hell's Fire",
           "image": "images/music.png",
           "link": "https://www.youtube.com/playlist?list=PLPfXrbtn3EgmZitRQf1X1iYwWW_nUF44L",
           "descpription": "Beats that match the ones of your heart",
           "reviews": [
               {                   
                    "name": "abc",
                    "rating": 3.5,
                    "review": "energetic",
                    "date": ""
               }
           ]
      },
      {
           "id": "2",
           "name": "Hindi",
           "image": "images/music.png",
           "link": "",
           "descpription": "",
           "reviews": [
               {                   
                    "name": "abc",
                    "rating": 4,
                    "review": "energetic",
                    "date": ""
               }            
           ]     
      },
      {
           "id": "3",
           "name": "Punjabi",
           "image": "images/music.png",
           "link": "https://www.youtube.com/playlist?list=PLPfXrbtn3Egnntch2thUO55YqPQgo4Qh7",
           "descpription": "",
           "reviews": [
               {                   
                    "name": "abc",
                    "rating": 4,
                    "review": "energetic",
                    "date": ""
               }            
           ]     
      },
      {
           "id": "4",
           "name": "Mix and Match",
           "image": "images/music.png",
           "link": "https://www.youtube.com/playlist?list=PLPfXrbtn3EglN5LVTETqH3ipRLfXmY6MB",
           "descpription": "",
           "reviews": [
               {                   
                    "name": "abc",
                    "rating": 5,
                    "review": "energetic",
                    "date": ""
               }            
           ]     
      }
   ]
}  ]
}`

我已经创建了angular个服务文件名mood.services.ts

import { Injectable } from '@angular/core';
import { Mood } from '../shared/mood';
import { Observable, of } from 'rxjs';
import { delay } from 'rxjs/operators';
import { map, catchError } from 'rxjs/operators';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { baseURL } from '../shared/baseurl';
import { ProcessHTTPMsgService } from './process-httpmsg.service';
@Injectable({
providedIn: 'root'
})
export class MoodService {

constructor(private http: HttpClient,
private processHTTPMsgService: ProcessHTTPMsgService) { }

getMoods(): Observable<Mood[]> {
  return this.http.get<Mood[]>(baseURL + 'moods')
  .pipe(catchError(this.processHTTPMsgService.handleError));
}

getMood(id: number): Observable<Mood> {
  return this.http.get<Mood>(baseURL+'moods/'+id)
  .pipe(catchError(this.processHTTPMsgService.handleError));
}

getMoodIds(): Observable<number[] | any> {
  return this.getMoods().pipe(map(moods => moods.map(mood => mood.id)))
  .pipe(catchError(error => error));
}

getMusicIds(): Observable<number[] | any> {
  return this.getMoods().pipe(map(musics => musics.map(music => music.id)))
}
}

这是我的 musicdetail.component.ts 文件,它将获取所选特定音乐的数据。

import { Component, OnInit, Inject } from '@angular/core';
import { Mood } from '../shared/mood';
import { Music } from '../shared/music';
import { Review } from '../shared/review';
import { MoodService } from '../services/mood.service';
import { Params, ActivatedRoute } from '@angular/router';
import { Location } from '@angular/common';
import { switchMap } from 'rxjs/operators';

@Component({
selector: 'app-musicdetail',
templateUrl: './musicdetail.component.html',
styleUrls: ['./musicdetail.component.scss']
})
export class MusicdetailComponent implements OnInit {

mood : Mood;
music: Music;
musicIds: string;
errMess: string;
prev : string;
next : string;
review: Review;

constructor(private moodservice: MoodService,
private route: ActivatedRoute,
private location: Location,
@Inject('BaseURL') private BaseURL) { }

ngOnInit(): void {
this.route.params.pipe(switchMap((params: Params) => {return this.moodservice.getMood(params['id']); 
}))
.subscribe(mood => {this.mood = mood;}, errmess => this.errMess = <any>errmess);
}

}

我在 music.component.ts 中使用 '[routerLink]="['/musicdetails', mood.id, music.id]"`,在音乐列表中,但我无法制定逻辑来获取特定音乐以显示其所有详细信息。我可以使用 getMood(id) 服务获取心情 ID,但无法对那种心情中的音乐做同样的事情。

你是这个意思吗?

getMusic(moodId: number, musicId: number): Observable<Music> {
  return this.getMood(moodId).pipe(
    map(mood => mood.musics.find(music => music.id == musicId)),
    // please note: not === since id is a string in your json, but a number param
  );
}