I'm quite new to typescript so forgive me if this is a noob question. I'm making a simple master detail application using angular cli. However i'm getting this error
argument of type 'number' is not assignable to a parameter of type 'string'
For as far is i can see however it's a string and not a number so i'm confused.
My code: model
export class Actor {
lastName: string;
profession: string;
bio: string;
url: string;
imageUri: string;
name: string;
id: string;
realName: string;
}
service
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));
}
}
component
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();
}
}
I also get the following error
cannot find name 'actor' in the component file
Your method expects a string
as argument, change it as
this.actorService.getActor(params.get('name'))).subscribe(hero => this.actor = actor);
also declare a variable inside the ts as
actor : any;
For as far is i can see however it's a string and not a number
+params.get('name')
is a number because unary +
in JavaScript converts its operand to a number .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.