我有问题'数字'类型的参数不能分配给'字符串'类型的参数

时间:2019-07-17 16:46:15

标签: angular typescript

在这种情况下,我有问题,问题是 不能将“数字”类型的参数分配给“字符串”类型的参数。

这是我的组件

import { Component, OnInit } from '@angular/core';
import { User } from './../../_models/user';
import { UserService } from './../../_services/user.service';
import { AlertifyService } from './../../_services/alertify.service';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-member-detail',
  templateUrl: './member-detail.component.html',
  styleUrls: ['./member-detail.component.css']
})
export class MemberDetailComponent implements OnInit {
  user: User;

  constructor(private userService: UserService, private alertify: AlertifyService, private route: ActivatedRoute) { }

  ngOnInit() {
    this.loadUser();
  }

  loadUser() {
    this.userService.getUser(+this.route.snapshot.params['id']).subscribe((user: User) => {  //error Argument of type 'number' is not assignable to parameter of type 'string'.
      this.user = user;
    }, error => {
      this.alertify.error(error);
    });
  }

}

这是我的_model

import { Photo } from './photo';

export interface User {
    id: number;
    username: string;
    knownAs: string;
    age: number;
    gender: string;
    created: Date;
    lastActive: Date;
    photoUrl: string;
    city: string;
    country: string;
    interests?: string;
    introduction?: string;
    lookingFor?: string;
    photos?: Photo[];
}

我希望['id']可以是数字参数

1 个答案:

答案 0 :(得分:0)

getUser()需要一个字符串。但是,您正在使用+将字符串参数转换为数字。因此,请删除+,或修复getUser()方法以将数字作为参数而不是字符串。 – [JB Nizet] [1],这是作品

谢谢