简体   繁体   English

预期 2 arguments,但得到 1.ts(2554)

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

相关问题 预期为 2 arguments,但得到 1.ts(2554) index.ts(54, 112):未提供“arg1”的参数 - Expected 2 arguments, but got 1.ts(2554) index.ts(54, 112): An argument for 'arg1' was not provided TS2554:预期 0 个参数,但得到 2 个 - TS2554: Expected 0 arguments, but got 2 在 Typescript 中传递 function 作为参数:预期为 0 arguments,但得到了 1.ts - Passing function as parameter in Typescript: Expected 0 arguments, but got 1.ts 错误 TS2554:需要 2-3 个参数,但得到 1 个 - error TS2554: Expected 2-3 arguments, but got 1 Ionic4错误TS2554:预期有1个参数,但得到0 - Ionic4 Error TS2554: Expected 1 arguments, but got 0 错误TS2554:预期3-4个参数,但得到5 - error TS2554: Expected 3-4 arguments, but got 5 错误TS2554,预期参数为2,但得到1 - Error TS2554, expected argument 2 but got 1 TypeScript 预期为 1 arguments,但得到 2 - TypeScript Expected 1 arguments, but got 2 TypeScript 错误 - 预期为 1-2 arguments,但得到 0 或更多。 TS2556 - TypeScript Error - Expected 1-2 arguments, but got 0 or more. TS2556 更新到6.3.3之后,我遇到了pipe()错误TS2557:预期至少有0个参数,但有1个或更多。 - after update to 6.3.3 I get in pipe() error TS2557: Expected at least 0 arguments, but got 1 or more.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM