在角度9的类型'void'中不存在属性'subscribe'

时间:2020-08-23 07:47:10

标签: angular

我在Angular 9上有一个项目。在我的Angular项目中,我想通过其emailpassword注册用户。我通过api在数据库中插入用户的电子邮件和密码。我正在使用的数据库是 Microsoft SQL server Mangament studio

我面临的错误是

Property 'subscribe' does not exist on type 'void'在我的signup.component.ts

由于存在类似的问题,但无法解决我的问题

signup.component.ts

import { Component, OnInit } from '@angular/core';
import {FormBuilder,Validators,FormGroup} from '@angular/forms';
import {SignupserviceService} from '../signup/signupservice.service';
import {Signup} from '../signup/signup';
import { from } from 'rxjs';


@Component({
  selector: 'app-signup',
  templateUrl: './signup.component.html',
  styleUrls: ['./signup.component.css']
})
export class SignupComponent implements OnInit {
  navbarOpen = false;
  regForm: FormGroup;
  datasaved:boolean= false;
  massage: string;

  toggleNavbar() {
    this.navbarOpen = !this.navbarOpen;
  }

  constructor(private formbuilder: FormBuilder, private signupservice:SignupserviceService
    ) { }

  ngOnInit(): void {
  }




  setFormState(): void {
    this.regForm = this.formbuilder.group({
      email: ['', [Validators.required]],
      password: ['', [Validators.required]]
    })
  }





  onSubmit() {
    
    let signup = this.regForm.value;

    this.createsignup(signup);
    this.regForm.reset();
  }




  
  createsignup(signup: Signup) {
    this.signupservice.createsignup(signup).subscribe(
      () => {
        this.datasaved = true;
        this.massage = "User Created";
       this.regForm.reset();
      }
    )
  }

}

signupserivce.service.ts (我的服务部分)

import { Injectable } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {observable, Observable}  from 'rxjs';
import {Signup} from './signup';
import { from } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class SignupserviceService {
  createsignup(signup: Signup) {
    throw new Error("Method not implemented.");
  }
  url="http://localhost:51180/";

  constructor(private http:HttpClient) { }

  createuser(user:Signup):Observable<Signup>
  {
     return this.http.post<Signup>(this.url+'api/users',user)
  }
}

我遇到的错误

 Property 'subscribe' does not exist on type 'void'.
    
    56     this.signupservice.createsignup(signup).subscribe(

您能帮我解决我的问题吗? 谢谢!!!

1 个答案:

答案 0 :(得分:0)

createsignup是在SignupserviceService

中实现的void方法
 createsignup(signup: Signup) {
    throw new Error("Method not implemented.");
  }

您在这里打电话

this.signupservice.createsignup(signup).subscribe(
      () => {
        this.datasaved = true;
        this.massage = "User Created";
       this.regForm.reset();
      }
    )

必须返回一个observable才能订阅 如果您尝试调用API,则http客户端方法将返回observables

return this.http.post<Signup>(this.url+'api/users',user)