Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 类型为'的参数;编号';不可分配给类型为';字符串';_Angular_Parameters - Fatal编程技术网

Angular 类型为'的参数;编号';不可分配给类型为';字符串';

Angular 类型为'的参数;编号';不可分配给类型为';字符串';,angular,parameters,Angular,Parameters,我对打字很陌生,所以如果这是一个noob问题,请原谅我。 我正在使用angular cli制作一个简单的master-detail应用程序。然而,我得到了这个错误 argument of type 'number' is not assignable to a parameter of type 'string' 就我所知,它是一个字符串,而不是一个数字,所以我很困惑 我的代码: 模型 服务 import { Injectable } from '@angular/core'; import

我对打字很陌生,所以如果这是一个noob问题,请原谅我。 我正在使用angular cli制作一个简单的master-detail应用程序。然而,我得到了这个错误

argument of type 'number' is not assignable to a parameter of type '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));
    }
}
我还得到了以下错误

cannot find name 'actor' in the component file

您的方法需要一个
字符串作为参数,请将其更改为

 this.actorService.getActor(params.get('name'))).subscribe(hero => this.actor = actor);
还将ts内的变量声明为

actor : any;
就我所知,它是一个字符串,而不是一个数字

+params.get('name')
是一个数字,因为JavaScript中的一元
+

这类似于“参数类型数字不能分配给参数类型字符串|函数”
 this.actorService.getActor(params.get('name'))).subscribe(hero => this.actor = actor);
actor : any;