javascript - 从本地json获取带有id的单个数据

标签 javascript json angular ionic-framework ionic4

I'm building list app with ionic 4 .all item list view work from local json , but single item with id not get from local json

app-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

const routes: Routes = [
  { path: '', redirectTo: 'home', pathMatch: 'full' },
  { path: 'home', loadChildren: './home/home.module#HomePageModule' },
  { path: 'details/:id', loadChildren: './details/details.module#DetailsPageModule' },
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

home.page.ts

import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';

import {map} from 'rxjs/operators';
import { Observable } from 'rxjs';
import { RecipiService } from '../services/recipi.service';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {
  //  jsons:any;


  constructor(private recipiservice:RecipiService ){
    this.recipiservice.getData();
  }
}

details.page.ts

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { RecipiService } from '../services/recipi.service';

@Component({
  selector: 'app-details',
  templateUrl: './details.page.html',
  styleUrls: ['./details.page.scss'],
})
export class DetailsPage implements OnInit {
  information = null ;

  constructor(private activatedRoute:ActivatedRoute,private recipiservice:RecipiService) { }

  ngOnInit() {
    let id = this.activatedRoute.snapshot.paramMap.get('id');
    this.recipiservice.getDetails(id).subscribe(result =>{
      console.log('details', result)
      this.information = result;
    });
  }

}

recipi.service.ts

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { HttpClient } from '@angular/common/http';
@Injectable({
  providedIn: 'root'
})
export class RecipiService {
  jsons: any;
  url = '/assets/resource.json';


    //json:any;
   constructor(private http:HttpClient) {}

  getData(){

    let data: Observable<any> = this.http.get(this.url);
    data.subscribe(results =>{
      // console.log(this.jsons);
        return this.jsons = results;
    })
}

getDetails(id){
   //console.log(id);
   return this.http.get(this.url); //how to get single item with id & this line code not complate . please help me for complate my code 
}
}

here all item get . but i went get to singel item

here all item get . but i went get to singel item

Like it, get single item with id

Like it, get single item with id

这是 ionic 4 应用程序。这是 ionic 4 应用程序。这是 ionic 4 应用程序。这是 ionic 4 应用程序。这是 ionic 4 应用程序。这是 ionic 4 应用程序。这是 ionic 4 应用程序。

最佳答案

您可以在服务代码中执行以下操作:

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { HttpClient } from '@angular/common/http';

import { map } from 'rxjs/operators'; // make sure to import the map operator


@Injectable({
  providedIn: 'root'
})
export class RecipiService {
  jsons: any;
  url = '/assets/resource.json';


  //json:any;
  constructor(private http:HttpClient) {}

  getData(){

    let data: Observable<any> = this.http.get(this.url);
    data.subscribe(results =>{
      // console.log(this.jsons);
        return this.jsons = results;
    })
  }

  getDetails(id){
    return this.http.get(this.url)
      .pipe(map(response) => {
        return response.find((item) => (item.id === id));
      })
  }

}

由于我使用 === 进行比较,请确保您传递给 getDetailsid每个项目中服务器响应的 >id 属性具有相同的数据类型。 IE。两者都应该是数字,或者两者都应该是字符串值。

Stackblitz Working Example

希望这有帮助。

关于javascript - 从本地json获取带有id的单个数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55339347/

相关文章:

javascript - 什么时候应该使用 javascript 框架库?

javascript - Chrome 多功能框特殊字符抛出错误

angular - 在 Angular 6 中从控制台隐藏 IE 警告

javascript - AmplifyJS 状态错误 TypeError : Cannot read property 'state' of undefined

javascript - 有没有办法获取从 npm 运行的模块中的 package.json 属性?

javascript - jQuery 代码需要转换为 vanilla 以便我可以在 Angular 中使用

javascript - 在 Backbone/Require 应用程序中使用 Router 和 ineriew 渲染嵌套 View

javascript - 来自多个 "active"客户端的 AppEngine 应用程序的可扩展轮询?

javascript - 将 req.body 属性映射到对象键

android - 如何使用 Retrofit 发布复杂的 JSON 参数