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

时间:2019-04-23 12:07:14

标签: angular typescript ionic-framework

所以我收到此错误: 类型'{“ username:”:字符串的参数; “密码”:字符串; }'不能分配给'string'类型的参数

我的代码:

export class LoginPage implements OnInit {

  responseData : any;
  userData = {"username:":"", "password":""};

  constructor(private menu: MenuController, private authloginService: AuthloginService, public navCtrl : NavController ) {   
  }

  fazerLogin(){

     this.authloginService.logarConta(this.userData, 'login').then((result)=>{ // error here
      this.responseData = result;
      console.log(this.responseData);
      this.navCtrl.navigateForward('home')

     });

EDIT1:具有函数logarConta的AuthloginService:

export class AuthloginService {



  private API_URL: 'https://myapi.com/api';

  constructor(public http: HttpClient) { }

  logarConta(email: string, password:string){
    return new Promise((resolve, reject) =>{
      var data = {
        name: name,
        email: email,
        password: password,
        message: 'Test',

      };

      this.http.post(this.API_URL + 'login', data)
        .subscribe((result: any) =>{
          resolve(result.json())
        }, 
        (message) =>{
          reject(message.json())
        })
    });

3 个答案:

答案 0 :(得分:2)

这应该是:

 HttpClient client = new HttpClient();
            string x = "{'IDName','regnum'},{'IDValue','112233'}";
            var Keys = new Dictionary<string, string>
            {
                   { "ServiceKey", "hello" },
                   { "PractitionerIdentity",x}
                };

        var content = new FormUrlEncodedContent(Keys);
        var response = await client.PostAsync("https://apiurl", content);
        var responseval = await response.Content.ReadAsStringAsync();

但是,我不确定此this.authloginService.logarConta(this.userData.username, this.userData.password) .then((result)=>{ this.responseData = result; console.log(this.responseData); this.navCtrl.navigateForward('home') }); 方法是否正确。因为您有一个logarConta参数,并且您的email没有电子邮件密钥。您应该检查这是否正确。

答案 1 :(得分:2)

非常感谢您的分享。问题是您有一个对象userData,该对象具有2个字符串属性用户名和密码。您的logarConta函数期望使用2个字符串变量的电子邮件和密码,但是您仅传递了1个对象。您可以通过将呼叫更改为以下形式来解决此问题:

  this.authloginService.logarConta(this.userData.username, this.userdata.password).then((result)=>{ // error here
  this.responseData = result;
  console.log(this.responseData);
  this.navCtrl.navigateForward('home')

 });

这将正确地将对象的属性映射到函数期望的变量。

答案 2 :(得分:-1)

this.authloginService.logarConta(...this.userData, 'login')