【问题标题】:Property 'subscribe' does not exist on type '"void"类型“无效”上不存在属性“订阅”
【发布时间】:2022-01-12 08:33:55
【问题描述】:

您好,我正在开发一个 Angular Web api 项目,我在 subscribe 关键字上收到此错误,也导入了 observable,但错误是相同的。因此,如果有人在此之前遇到过这种情况,请提供帮助。

employee.component.ts

import { Component, OnInit } from '@angular/core';
import { NgForm } from '@angular/forms';
import { EmployeeService } from 'src/app/shared/employee.service';
import 'rxjs/add/operator/maps';
import { Observable } from 'rxjs';

@Component({
  selector: 'app-employee',
  templateUrl: './employee.component.html',
  styleUrls: ['./employee.component.css']
})
export class EmployeeComponent implements OnInit {
 
  constructor(public service: EmployeeService) { }

  ngOnInit(): void {
    this.resetForm();
  }
  resetForm(form?: NgForm) {
    if (form != null)
      form.resetForm();
    this.service.formData = {
      EmployeeID: null,
      FullName: '',
      Position: '',
      EMPCode: '',
      Mobile: ''
}
  }
  onSubmit(form: NgForm) {
    if (form.value.EmployeeID == null)
      this.insertRecord(form);
  }
      insertRecord(form: NgForm) {
        this.service.postEmployee(form.value).subscribe(res  => {           
     
         this.resetForm(form);
       
        });
  }
}

【问题讨论】:

  • 您至少需要分享postEmployee函数的代码,否则我们无法为您提供帮助
  • 您是否在您的postEmployee() 服务上使用了return
  • 是的,使用 return 方法后,我在 this.service.postEmployee 行代码出现错误: insertRecord(form: NgForm) { return{ this.service.postEmployee(form.value).subscribe( (res: any[]): void=> { this.toastr.success('插入成功', '员工详情'); this.resetForm(form); this.service.refreshList(); }), } } @ KibéM.C

标签: angular observable subscribe


【解决方案1】:

试试这个...

import { map } from 'rxjs/operators';
import { Observable } from 'rxjs';

insertRecord(form: NgForm):Observable<any> {
 return this.service.postEmployee(form.value).pipe(map(((res: any[]): void=> { 

this.toastr.success('Inserted successfully', 'Employee Details'); 

this.resetForm(form); this.service.refreshList(); }), } } 

【讨论】:

    猜你喜欢
    • 2019-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-20
    • 2018-12-02
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    相关资源
    最近更新 更多