![](/img/trans.png)
[英]Argument of type 'string' is not assignable to parameter of type 'number'
[英]argument of type 'number' is not assignable to a parameter of type 'string'
我對打字稿很新,所以請原諒我,如果這是一個菜鳥問題。 我正在使用angular cli制作一個簡單的主細節應用程序。 但是我收到了這個錯誤
argument of type 'number' is not assignable to a parameter of type 'string'
到目前為止,我可以看到它是一個字符串,而不是一個數字,所以我很困惑。
我的代碼:模特
export class Actor {
lastName: string;
profession: string;
bio: string;
url: string;
imageUri: string;
name: string;
id: string;
realName: string;
}
服務
import { Injectable } from '@angular/core';
import { Actor } from '../model/actor';
// import { ACTORS } from './mock-actors';
import { Http } from '@angular/http';
import 'rxjs/add/operator/toPromise';
@Injectable()
export class ActorService {
private actorsUrl = '/app/persons.json';
constructor(private http: Http) { }
getActors(): Promise<Actor[]> {
return this.http.get(this.actorsUrl)
.toPromise().then(response => <Actor[]>response.json().personList)
/*.toPromise().then(response => response.json().personList as Actor[])*/
.catch(this.handleError);
}
private handleError(error: any): Promise<any> {
console.error('Error: ', error);
return Promise.reject(error.message);
}
getActor(name: string): Promise<Actor> {
return this.getActors().then(actors => actors.find(actor => actor.name === name));
}
}
零件
import { Component, OnInit } from '@angular/core';
import { Actor } from '../../model/actor';
import { ActivatedRoute, ParamMap } from '@angular/router';
import { Location } from '@angular/common';
import { ActorService } from '../../service/actor.service';
import 'rxjs/add/operator/switchMap';
@Component({
/*selector: 'actor-detail',*/
templateUrl: './detail-index.component.html'
})
export class ActorDetailComponent implements OnInit{
actor: Actor;
constructor(
private actorService: ActorService,
private route: ActivatedRoute,
private location: Location
) {}
ngOnInit(): void {
this.route.paramMap.switchMap((params: ParamMap) =>
this.actorService.getActor(+params.get('name'))) // argument of type 'number' is not assignable to a parameter of type 'string'
.subscribe(hero => this.actor = actor);
}
goBack(): void {
this.location.back();
}
}
我也得到以下錯誤
cannot find name 'actor' in the component file
您的方法需要一個string
作為參數,將其更改為
this.actorService.getActor(params.get('name'))).subscribe(hero => this.actor = actor);
也在ts中聲明一個變量
actor : any;
因為我可以看到它是一個字符串,而不是一個數字
+params.get('name')
是一個數字,因為JavaScript中的一元+
將其操作數轉換為數字 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.