简体   繁体   English

如何通过 id 获取 api url?(角度)

[英]How to pass id to get api url?(Angular)

I have to pass an id property from GET api to another GET api For example... I'm doing something like this..我必须将一个 id 属性从 GET api 传递给另一个 GET api 例如......我正在做这样的事情......

https://jsonplaceholder.typicode.com/posts/${value} 

where ' ${value} ' has to be replaced by 1,2,... But this is showing me 404 not found error.. But if I try individually like this其中' ${value} '必须替换为 1,2,... 但这向我显示 404 not found 错误.. 但是如果我像这样单独尝试

https://jsonplaceholder.typicode.com/posts/1

I'm able to fetch results.. Please help me resolve this issue..我能够获取结果..请帮我解决这个问题..

.ts file .ts 文件

viewPosts(value){
   console.log(value);
  this.apiService.getPosts(value).subscribe((result) => {
       console.log(result);
        ......

});  

 }

ApiService file: ApiService 文件:

getPosts(value:any):Observable<any>{

    const api='https://jsonplaceholder.typicode.com/posts/${value}';
    return this.http.get(api);
  }

you just have to change this code.您只需要更改此代码。

getPosts(value:any):Observable<any>{
  const api = `https://jsonplaceholder.typicode.com/posts/${value}`;
  return this.http.get(api);
}

you have to use template literal .你必须使用模板文字

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

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