我得到了这个错误: TS2339:属性‘订阅’在'void‘类型上不存在。
contacts.component.ts
import { Component, OnInit } from '@angular/core';
import { NgForm } from '@angular/forms';
import { Observable } from 'rxjs';
import { ContactsService } from '../contacts.service';
@Component({
selector: 'app-contacts',
templateUrl: './contacts.component.html',
styleUrls: ['./contacts.component.scss']
})
export class ContactsComponent implements OnInit {
constructor(public contactService:ContactsService) { }
msgTrue=false;
updateContact(contactId: number){
const newData={id:contactId,firstName:'Roa',lastName:'Hugo'};
this.contactService.updateContact(contactId,newData).subscribe(data=>{
this.msgTrue=true;
});
}
}contacts.service.ts
import { Injectable } from '@angular/core';
import { HttpClient,HttpHeaders,HttpParams } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class ContactsService {
constructor(private httpClient:HttpClient) { }
updateContact(contactId:number,updateBody:any){
const endpoint='http://localhost:3000/contacts/'+contactId;
this.httpClient.put(endpoint,updateBody);
}
}tsconfig.json
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"forceConsistentCasingInFileNames": true,
"strict": false,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"moduleResolution": "node",
"importHelpers": true,
"target": "es2020",
"module": "es2020",
"lib": [
"es2020",
"dom"
]
},
"angularCompilerOptions": {
"enableI18nLegacyMessageIdFormat": true,
"strictInjectionParameters": true,
"strictInputAccessModifiers": true,
"strictTemplates": true
}
}我在contacts.component.ts上得到了错误
行: this.contactService.updateContact(contactId,newData).subscribe(data=>{
错误TS2339:属性“订阅”在类型'void‘上不存在。
我知道“订阅”属性上有很多问题在'void‘类型中不存在,但它们似乎都是通过输入“返回”请求来解决的;我尝试了这个解决方案,但是它不起作用。
发布于 2022-10-10 16:17:05
这可能是因为您的服务函数不返回可观察到的。您需要从updateContract返回可观察到的:
updateContact(contactId:number, updateBody:any): Observable<Contract>{
const endpoint='http://localhost:3000/contacts/'+contactId;
return this.httpClient.put<Contract>(endpoint,updateBody);
}https://stackoverflow.com/questions/74017727
复制相似问题