角度服务组件错误 TS2339:类型"承诺<Object>"上不存在属性'subscribe'



我正在尝试通过为教区创建一个Web应用程序来学习角度。在products-list.component.ts我有删除方法,该方法在形式上似乎是正确的,但在npm start阶段,它会出现错误代码

TS2339:类型"承诺"上不存在属性"订阅"。

如果我不尊重转发问题的正式规则,我深表歉意,但我是新手,也许我错误地记录了问题。感谢您的耐心等待。莫雷诺

onDelete方法出错

在其他类中,我使用类似的代码,并且没有出现过此类错误。列表上是否有任何进口产品?谢谢无限

import { Component, OnInit, SystemJsNgModuleLoader } from '@angular/core';
import { NgForm } from '@angular/forms';

import { Prodotti } from 'src/app/model/prodotti.model';
import { Router, ActivatedRoute } from '@angular/router';
import { JsonPipe } from '@angular/common';
import { ProdottiListService} from 'src/app/features/prodotti/components/prodotti-list/prodotti-list.service';
import { ToastrService } from 'ngx-toastr';

let Header_Msg = "Gestione Prodotti";

@Component({
  selector: 'app-prodotti-list',
  templateUrl: './prodotti-list.component.html',
  styleUrls: ['./prodotti-list.component.css']
})
export class ProdottiListComponent implements OnInit {
  constructor(private service: ProdottiListService, private toastr: ToastrService) {
   }
  ngOnInit() {
    this.service.refreshList();
  }
  populateForm(emp: Prodotti) {
    this.service.formData = Object.assign({}, emp);
  }
  onDelete(id: number) {
    if (confirm('Confermi la cancellazione del Record ?'))  {
        this.service.deleteProdotti(id).subscribe(res => {
          this.service.refreshList();
          this.toastr.warning('Cancellazione eseguita con successo', Header_Msg);
        })
    }
  }
}
src/app/features/prodotti/

components/prodotti-list/prodotti-list.component.ts(41,41( 中的错误:错误 TS2339:类型"Promise"上不存在属性"订阅"。

您需要

确保服务方法返回Observable而不是promise

在您的情况下,ProdottiListService具有返回promisedeleteProdotti,因此您应该使用.then(..)而不是.subscribe()

更新服务方法以返回observable,或者不要在组件中使用.subscribe

this.service.deleteProdotti(id).then(res => {
          this.service.refreshList();
          this.toastr.warning('Cancellazione eseguita con successo', Header_Msg);
        })

相关内容

最新更新