简体   繁体   English

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

[英]Argument of type 'X' is not assignable to parameter of type 'string'

So I'm getting this error: Argument of type '{ "username:": string; 所以我得到这个错误:类型为'{“ username:”:string;的参数。 "password": string; “密码”:字符串; }' is not assignable to parameter of type 'string' }'不可分配给'string'类型的参数

My code: 我的代码:

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: AuthloginService with the function logarConta: EDIT1:AuthloginService与函数logarConta:

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())
        })
    });

This should be: 应该是:

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

But, Im not sure if this logarConta method is good. 但是,我不确定此logarConta方法是否正确。 Because you have there a email argument, and your userData = {"username:":"", "password":""}; 因为您有一个email参数,并且您的userData = {"username:":"", "password":""}; has not email key. 没有电子邮件密钥。 You should check if this is correct approach. 您应该检查这是否正确。

Awesome thanks for sharing. 非常感谢您的分享。 The problem is that you have an object userData which has 2 string properties username and password. 问题是您有一个对象userData ,它具有2个字符串属性username和password。 Your logarConta function is expecting 2 string variables email and password, but you are just passing 1 object. 您的logarConta函数期望使用2个字符串变量的电子邮件和密码,但是您仅传递了1个对象。 You can fix this by changing your call to look like this: 您可以通过将呼叫更改为以下形式来解决此问题:

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

 });

This will properly map the properties of the object to the variables the function is expecting. 这将正确地将对象的属性映射到函数期望的变量。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Tslint 类型“x”的参数不可分配给类型“字符串”的参数 - Tslint Argument of type “x” is not assignable to parameter of type 'string' 日期类型的参数不能分配给字符串类型的参数 - Argument of type date is not assignable to parameter of type string “文件”类型的参数不能分配给“字符串”类型的参数 - Argument of type 'File' is not assignable to parameter of type 'string' 类型为AbstractControl的参数不能分配给字符串类型的参数 - argument of type AbstractControl is not assignable to parameter of type string “字符串”类型的参数不能分配给“数字”类型的参数 - Argument of type 'string' is not assignable to parameter of type 'number' 'any[]' 类型的参数不能分配给'string' 类型的参数 - Argument of type 'any[]' is not assignable to parameter of type 'string' “用户”类型的参数不能分配给“字符串”类型的参数 - Argument of type 'User' is not assignable to parameter of type 'string' “对象”类型的参数不能分配给“字符串”类型的参数 - Argument of type 'Object' is not assignable to parameter of type 'string' 类型为'number'的参数不能赋值给'string'类型的参数 - argument of type 'number' is not assignable to a parameter of type 'string' “日期”类型的参数不能分配给“字符串”类型的参数 - Argument of type 'Date' is not assignable to parameter of type 'string'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM