我一直在使用这个 api 构建一个 angular2 应用程序:https ://api.coinmarketcap.com/v1/ticker/
这个 api 返回一个字典数组(样本):
[
{
"id": "bitcoin", <------ Wanna use this id
"name": "Bitcoin",
"symbol": "BTC",
"rank": "1",
"price_usd": "9347.32",
"price_btc": "1.0",
"24h_volume_usd": "7926060000.0",
"market_cap_usd": "158936099373",
"available_supply": "17003387.0",
"total_supply": "17003387.0",
"max_supply": "21000000.0",
"percent_change_1h": "-0.24",
"percent_change_24h": "1.1",
"percent_change_7d": "6.62",
"last_updated": "1524926675"
},
我正在使用路由参数转到不同的页面,如下所示:
<tr *ngFor="let curr of currency" [routerLink]="['/currdetail',curr.id]"> <------ Here
<th scope="row">{{ curr.rank }}</th>
<td>{{curr.name}}</td>
<td>{{curr.symbol}}</td>
<td>{{curr.market_cap_usd}}</td>
<td>{{curr.price_usd}}</td>
<td>{{curr.price_btc}} </td>
<td>{{curr.available_supply}} </td>
<td>{{curr['24h_volume_usd']}} </td>
<td>{{curr.percent_change_1h}} </td>
<td>{{curr.percent_change_24h}}</td>
<td>{{curr.percent_change_7d}} </td>
</tr>
现在我已经编写了这样的路由并包含在我的路由模块中:
export const routes: Routes = [
{ path: 'table', component: TableComponent },
{ path: 'currdetail/:id', component: CurrdetailComponent },
{ path: '', redirectTo: '/table', pathMatch: 'full' },
];
Currdetail 组件如下所示:
import { Component, OnInit } from '@angular/core';
import { CrypdataService } from '../crypdata.service'
import { Params, ActivatedRoute } from '@angular/router';
import { Location } from '@angular/common';
@Component({
selector: 'app-currdetail',
templateUrl: './currdetail.component.html',
styleUrls: ['./currdetail.component.css'],
providers: [CrypdataService],
})
export class CurrdetailComponent implements OnInit {
private curr;
constructor(private crypdataservice: CrypdataService, private route: ActivatedRoute, private location: Location) { }
ngOnInit() {
let id = +this.route.snapshot.params['id'];
this.curr = this.crypdataservice.getcurr(id);
}
goBack(): void{
this.location.back();
}
}
这是密码数据服务文件:
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class CrypdataService {
private baseUrl: string = 'https://api.coinmarketcap.com/v1/ticker/';
constructor(private http: Http) { }
getcurr(id : string){
return this.http.get(`https://api.coinmarketcap.com/v1/ticker/`+id).map((res:Response) => res.json());
}
getall(){
return this.http.get(`https://api.coinmarketcap.com/v1/ticker/`).map((res:Response) => res.json());
}
}
它给出了以下错误:
ERROR in src/app/currdetail/currdetail.component.ts(20,44): error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
我从未将 id 指定为数字,因此无法弄清楚出了什么问题。谢谢阅读。
我想使用 api 获取特定货币的详细信息:https : //api.coinmarketcap.com/v1/ticker/bitcoin/ <---- 这是 id
删除+
你的id
声明:
let id = this.route.snapshot.params['id'];
这样当你调用getcurr(id)
TypeScript 时不会认为这id
是一个数字
一元加号 (+) (文档)
一元加号运算符位于其操作数之前并计算其操作数,但尝试将其转换为数字(如果尚未转换)。尽管一元否定 (-) 也可以转换非数字,但一元加号是将某些内容转换为数字的最快和首选方式,因为它不对数字执行任何其他操作。它可以转换整数和浮点数的字符串表示,以及非字符串值 true、false 和 null。支持十进制和十六进制(“0x”前缀)格式的整数。支持负数(但不支持十六进制)。如果它无法解析特定值,它将评估为 NaN。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句