简体   繁体   English

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

[英]Property 'subscribe' does not exist on type 'void' in angular 9

I have a project on Angular 9. In my angular project i want register a user through his email and password .我有一个关于 Angular 9 的项目。在我的 angular 项目中,我想通过他的emailpassword注册一个用户。 I am inserting users email and password in database through api.我正在通过 api 在数据库中插入用户电子邮件和密码。 The database i am using is Microsoft SQL server Mangament studio .我使用的数据库是Microsoft SQL server Mangament studio

The error i am facing is我面临的错误是

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

As there are similar questions but it cann't resolve my problem因为有类似的问题,但它不能解决我的问题

signup.component.ts注册.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 (my service part) signupservice.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)
  }
}

the error i have got我得到的错误

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

Can you help me where i am going wrong.你能帮我哪里出错了。 Thank you!!!!谢谢!!!!

createsignup is a void method implemented in the SignupserviceService createsignup是在SignupserviceService实现的 void 方法

 createsignup(signup: Signup) {
    throw new Error("Method not implemented.");
  }

which you are calling here你在这里打电话

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

It must return an observable in order to subscribe If You are trying to call an API, the http client methods returns observables它必须返回一个 observable 才能订阅如果您尝试调用 API,http 客户端方法将返回 observables

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM