要解决这个问题,可以使用Angular 7中的拦截器来修改post请求的数据。以下是一个示例的代码:
import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable()
export class IdInterceptor implements HttpInterceptor {
  intercept(request: HttpRequest, next: HttpHandler): Observable> {
    if (request.method === 'POST') {
      const modifiedRequest = request.clone({
        body: { id: request.body.id } // 只保留id属性
      });
      return next.handle(modifiedRequest);
    }
    return next.handle(request);
  }
}
  
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { IdInterceptor } from './interceptor'; // 导入拦截器
import { AppComponent } from './app.component';
@NgModule({
  declarations: [AppComponent],
  imports: [BrowserModule, HttpClientModule],
  providers: [
    { provide: HTTP_INTERCEPTORS, useClass: IdInterceptor, multi: true } // 添加拦截器到providers数组中
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }
这样,每次发送post请求时,拦截器都会检查请求的方法是否为POST,并仅保留id属性。其他属性将被忽略。
请注意,以上代码中的拦截器只适用于post请求。如果您还想应用相同的逻辑于其他类型的请求,可以在拦截器中进行相应的修改。