“数字”类型的参数不能分配给 angular2 中“字符串”类型的参数。

阿比吉特·辛格

我一直在使用这个 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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

“数字”类型的参数不能分配给“字符串”类型的参数 - Typescript 和 Angular

Angular4:编译失败:参数类型{路径:字符串,组件 HomeComponent}[] 不能分配给参数类型路由

Angular 5 错误 TS2345:“数字”类型的参数不可分配给“字符串”类型的参数

“数字”类型的参数不能分配给“字符串”类型的参数

类型“ {}”的参数不能分配给Angular 8中的类型的参数

类型 X 的参数不能分配给 Angular 7 中类型 X 错误的参数

Angular2-类型'QueryList <ElementRef>'的参数不能分配给类型'string'的参数

'string | 类型的参数 number' 不能分配给类型为 'string' 的参数。“数字”类型不能分配给“字符串”类型

Angular 6-参数类型订阅不能分配给参数类型Array <any>

Angular + Chart.js /类型'ChartPoint'的参数不能分配给'number&ChartPoint'类型的参数

Angular - 'IContact[] 类型的参数 | undefined' 不能分配给类型为 'IContact[]' 的参数

Angular - “IPgs”类型的参数不能分配给“IPgs[]”类型的参数

Angular 4/5 HttpClient:字符串类型的参数无法分配给“ body”

Angular Tour of Heroes,HTTP 请求:“英雄”类型的参数不可分配给“字符串”类型的参数

参数类型字符串?不能分配给参数类型“字符串”

消息错误:参数类型“字符串?” 不能分配给参数类型“字符串”

不能使用 ParseInt 将“数字”类型的参数分配给“字符串”类型的参数

“数字”类型的参数不能分配给“字符串”类型的参数。将旧项目导入新项目时

RxDB Angular2-cli'Promise <void>'不能分配给'Promise <any>'类型的参数

'{ keyPrefix: string; 类型的 ReactJS Typescript 参数 }' 不能分配给字符串类型的参数

“字符串”类型的参数不能分配给“{ email: string;”类型的参数。}' NGXS

类型编号的参数不能分配给字符串类型的参数

“日期”类型的参数不能分配给“字符串”类型的参数

提取:“字符串”类型的参数| 未定义”不能分配给“ RequestInfo”类型的参数

参数类型“对象?” 不能分配给参数类型“字符串”

“用户”类型的参数不能分配给“字符串”类型的参数

类型为AbstractControl的参数不能分配给字符串类型的参数

类型“ X”的参数不能分配给类型“字符串”的参数

“字符串类型的参数不能分配给自定义类型的参数”的难看的问题