角度登录请求

时间:2019-03-19 13:03:21

标签: angular request angular7

我是Angular的新手,并且想要建立一个登录名。但是我不知道如何发出登录请求。我搜索了类似的问题,但只发现了较旧的问题。谢谢您的帮助

我的表单:

 <form [formGroup]="firstFormGroup" (submit)="firstForm()" class="formLogin">
  <mat-form-field class="formFieldName">
    <input matInput placeholder="name" type="text" formControlName="name">
    <mat-icon matSuffix>person</mat-icon>
  </mat-form-field>

  <mat-form-field>
    <input matInput placeholder="password" type="password" formControlName="password">
    <mat-icon matSuffix>lock</mat-icon>

  </mat-form-field>
  <div>
    <button mat-raised-button color="primary" type="submit" class="loginButton">Log in</button>
    <div>
      <mat-icon class="iconHelp">help_outline</mat-icon>
    </div>
  </div>
</form>

Logincomponent.ts

 firstForm() {
     if (this.firstFormGroup.valid) {
        console.log(this.firstFormGroup.value);
        console.log(this.loginUserData);
     } else {
         this.snackbar.open('Error!', null, {duration: 3000});
     }
 }

服务:

constructor(private http: HttpClient) { }
sendData(user: any) {
    this.http.post('https://.../token', user, {withCredentials: true});
}

最诚挚的问候拉里

1 个答案:

答案 0 :(得分:0)

尝试一下。

在使用中

sendData(user: any)  {
    return this.http.post('https://.../token', user);
}

Logincomponent.ts

constructor(private serviceName:ServiceName){}

firstForm() {
   if (this.firstFormGroup.invalid) {
      this.snackbar.open('Error!', null, {duration: 3000});
      return;
   } 
   this.serviceName.sendData(this.firstFormGroup.getRawValue())
   .subscribe((response: any) => {
       console.log("On response");
   },error => {
       console.log("On error");
   })
 }