[英]Expected 2 arguments, but got 1.ts(2554)
Good morning friends, sorry for the inconvenience I am doing practices to learn and I was doing a login, but the problem is that I am trying to connect it to an api and it does not make the connection, it gives me a super strange error in the login component Here I attach the login.component朋友们早上好,很抱歉给我带来不便,我正在练习学习,我正在登录,但问题是我试图将它连接到 api 并且它没有建立连接,它给了我一个超级奇怪的错误在登录组件这里我附上 login.component
import { Component, } from '@angular/core';
import { AppserviceService } from '../../services/appservice.service';
import { NgForm } from '@angular/forms';
import { AppsResp } from '../../interfaces/interfaces';
import { FormsModule } from '@angular/forms';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
})
export class LoginComponent {
email:string ='';
password:string='';
constructor(public AppserviceService: AppserviceService) { }
login() {
const user = {email: this.email, password: this.password};
this.AppserviceService.login(user).subscribe( data => {
console.log(data);
});
}
}
the error that fits is the following_ "Expected 2 arguments, but got 1.ts(2554) appservice.service.ts(15, 26): An argument for 'password' was not provided."适合的错误如下_“预期为 2 arguments,但得到 1.ts(2554) appservice.service.ts(15, 26):未提供“密码”的参数。”
Here I attach the app services service, which is where the origin of the error marks me在这里我附上应用服务服务,这是错误的来源标记我的地方
import { HttpClient } from '@angular/common/http';
import { Injectable, Query } from '@angular/core';
import { Observable } from 'rxjs';
import { AppsResp, Registro } from '../interfaces/interfaces';
@Injectable({
providedIn: 'root'
})
export class AppserviceService {
constructor(private http: HttpClient) { }
login ( email: string, password: string ){
const body = {email,password}
return this.http.post <AppsResp>("http://apitest.e-bango.com/api/auth/login" , body );
}
}
Likewise, I can't find the correct logic to insert the registry component in my service, can you help me?同样,我找不到在我的服务中插入注册表组件的正确逻辑,你能帮帮我吗? and explain in as much detail as possible what I'm doing wrong?
并尽可能详细地解释我做错了什么? Thank you
谢谢
I think you have 2 ways to resolve this problem我认为您有两种方法可以解决此问题
1: Your input to function login is not correct 1:您输入的功能登录不正确
login() {
const user = {email: this.email, password: this.password};
this.AppserviceService.login(user).subscribe( data => {
console.log(data);
});
}
Change to改成
login() {
this.AppserviceService.login(email: this.email, password: this.password).subscribe( data => {
console.log(data);
});
}
2: Change the function login 2:更改登录功能
login ( email: string, password: string ){
const body = {email,password}
return this.http.post <AppsResp>("http://apitest.e-bango.com/api/auth/login" , body );
}
to至
login ( {email: string, password: string }){
const body = {email,password}
return this.http.post <AppsResp>("http://apitest.e-bango.com/api/auth/login" , body );
}
just change login method in appserviceService只需更改 appserviceService 中的登录方法
login ( body ){ return this.http.post <AppsResp>("http://apitest.e-bango.com/api/auth/login" , body ); }
its because your .login from AppserviceService is accepts or excepts 2 arguments, this is email and password.这是因为您来自 AppserviceService 的 .login 接受或排除 2 个参数,这是电子邮件和密码。 But you gonna put there user object
但是你要把用户对象放在那里
try this尝试这个
export class LoginComponent {
email:string ='';
password:string='';
constructor(public AppserviceService: AppserviceService) { }
login() {
this.AppserviceService.login(this.email, this.password).subscribe( data => {
console.log(data);
});
}
}
Please check destructuring in the docs请检查文档中的解构
login ({email, password}){
...
}
You can also give values by defect您还可以按缺陷给出值
login ({email="",password=""}){
...
}
And you can indicate the type你可以指出类型
login(({email="",password=""}:{email?:string,password?:string}){
...
}
If you use "optional parameters" use ?
如果您使用“可选参数”,请使用
?
to indicate is optional指示是可选的
BTW: you can pass the user itself顺便说一句:您可以传递用户本身
login(user:{email?:string,password?:string}){
return this.http.post(...,user);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.