简体   繁体   English

属性“ requestOptions”在类型上不存在

[英]Property 'requestOptions' does not exist on type

I want to send data to WebAPI service from Angular web client .In this post method im getting an compile error .'[ts] Property 'requestOptions' does not exist on type 'UserService'(Current Typescrpipt) . 我想将数据从Angular Web客户端发送到WebAPI服务。在此post方法中,我收到了编译错误。'[ts]属性'requestOptions'在'UserService'类型上不存在(Current Typescrpipt)。

import { Injectable } from '@angular/core';
import { Http,Response } from '@angular/http';
import { Observable } from 'rxjs/Rx'; ///Allow clients to subscribbe responces asnc and add Rx when handling errors catch block
import {User} from './user';
import {HttpClientModule} from '@angular/common/http';
import { HttpClient } from '@angular/common/http';
import { HttpHeaders } from '@angular/common/http';
import 'rxjs/Rx';


@Injectable()
export class UserService {

  constructor( private http : Http) { 

  }


  RegisterUser(user :User) {

    const headerDict = {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
        'Access-Control-Allow-Headers': 'Content-Type',
      }

      const requestOptions = {                                                                                                                                                                                 
        headers: new HttpHeaders(headerDict), 
      };

      const data = JSON.stringify(User);
      return this.http.post("http://localhost:53269/Api/RegisterUser", data, this.requestOptions );

      }



}

Use HttpClientModule with its HttpClient abstraction. 使用HttpClientModule及其HttpClient抽象。 Also you don't need to use JSON.stringify - it is extra 另外,您不需要使用JSON.stringify这是额外的

@Injectable()
export class UserService {

   constructor(private httpClient: HttpClient) { }

   RegisterUser(user: User) {

      const headerDict = {
          'Content-Type': 'application/json',
          'Accept': 'application/json',
          'Access-Control-Allow-Headers': 'Content-Type',
      };

      const requestOptions = {                                                                                                                                                                                 
          headers: new HttpHeaders(headerDict), 
      };

      return this.httpClient.post("http://localhost:53269/Api/RegisterUser", user, requestOptions);   
  }    

}

One note. 一注。 Not related to the actual issue. 与实际问题无关。 Don't import anything from rxjs/Rx . 不要从rxjs/Rx导入任何内容。 This will make your bundle huge. 这将使您的捆绑包变得巨大。 If you want to import Observable - just import like 如果要导入Observable只需像导入

import { Observable } from 'rxjs/Observable'

If you want some operator - import it separately from rxjs/operators or rxjs/observables/ 如果您需要一些运算符,请与rxjs/operatorsrxjs/observables/分开导入

Use eihter HttpClientModule with HttpClient or HttpModule with Http - which is deprecated now 使用带有HttpClient eihter HttpClientModule或带有Http HttpModule现在已弃用

you can try this 你可以试试这个

  const headersOption = new HttpHeaders().set('Content-Type': 'application/json',
    'Accept': 'application/json',
    'Access-Control-Allow-Headers': 'Content-Type')

  const data = JSON.stringify(User);
  return this.http.post("http://localhost:53269/Api/RegisterUser", data, {headers:headersOption});

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

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