[英]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.