简体   繁体   English

如何在 angular 7 中的激活链接中获取 url 参数并从服务类调用 rest api

[英]How to get url parameter in activated link in angular 7 and call rest api from service class

I want to fetch data using id field using Get parameters.我想使用 Get 参数使用 id 字段获取数据。 Follwing is my html url code which is redirecting to specific page but doesn't call the service to fetch rest api Follwing 是我的 html url 代码,它重定向到特定页面但不调用服务来获取 rest api

  <a [routerLink]="['/usedCars/detail', lists.id]" class="btn btn-danger">Read more...</a>

Below is my service.ts function and i have implemented import { HttpClient, HttpErrorResponse } from '@angular/common/http';下面是我的 service.ts 函数,我已经实现了import { HttpClient, HttpErrorResponse } from '@angular/common/http';

getcarDetail(id:string){
    return this.http.get<Autocardetail>(this.ServerUrl + 'autocardetail/'+id).pipe(
      catchError(this.handleError)
    );
  }

Below is my component.ts file where i am calling the service on ngOninit function.下面是我在 ngOninit 函数上调用服务的 component.ts 文件。

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

import { Router, ActivatedRoute, ParamMap} from '@angular/router';
import { Autocardetail } from '../autocardetail';
import { AutopostService } from '../autopost.service';

import { Observable } from 'rxjs';
import { switchMap } from 'rxjs/operators';

@Component({
  selector: 'app-autopostdetail',
  templateUrl: './autopostdetail.component.html',
  styleUrls: ['./autopostdetail.component.css']
})
export class AutopostdetailComponent implements OnInit {
  detail$: Observable<Autocardetail>;
  constructor(
        private route: ActivatedRoute,
        private router: Router,
        private autopostService: AutopostService
      ) {}

  ngOnInit() {
    this.detail$ = this.route.paramMap.pipe(
      switchMap((params: ParamMap) => {
        return this.autopostService.getcarDetail(params.get('id'))
    })
    );
  }
}

Below is my class file下面是我的类文件

export class Autocardetail {
  id: string;
  car_name: string;
  car_model: string;
  car_built: string;
  car_manufac_year: string;
}

Below is a postman example how a response look like,下面是一个邮递员示例,响应的样子,

{
    "car_id": "0",
    "car_name": "Nissan",
    "car_model": "Sunny",
    "car_built": "Nissan inc",
    "car_manufac_year": "2019",
    "usedcarimages": [
        "0_Nissan-1.jpg",
        "0_Nissan-2.jpg"
    ]
}

I am using this website as reference https://www.truecodex.com/course/angular-project-training/service-and-http-client-for-blog-detail-recent-blog-and-categories-angular我使用这个网站作为参考https://www.truecodex.com/course/angular-project-training/service-and-http-client-for-blog-detail-recent-blog-and-categories-angular

The only potential problem I think in your code is in the usage of swtichMap() .我认为您的代码中唯一潜在的问题是swtichMap()的使用。

Currently-目前-

switchMap((params: ParamMap) =>
  this.autopostService.getcarDetail(params.get('id'))
)

The code instruction this.autopostService.getcarDetail(params.get('id')) should be in the same line as that of swtichMap() if you are using arrow function or explicit return statement needs to be mentioned if you are breaking the line.代码指令this.autopostService.getcarDetail(params.get('id'))应该swtichMap()在同一行,如果你使用箭头函数或显式return语句,如果你打破行需要提到.

Correct ways-正确方法——

switchMap((params: ParamMap) => {
  return this.autopostService.getcarDetail(params.get('id'))
})

or或者

switchMap((params: ParamMap) => this.autopostService.getcarDetail(params.get('id')))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM