“header:Header;”类型的参数不可分配给“RequestOptionsArgs”类型的参数

Posted

技术标签:

【中文标题】“header:Header;”类型的参数不可分配给“RequestOptionsArgs”类型的参数【英文标题】:Argument of type 'header:Header;' is not assignable to parameter of type 'RequestOptionsArgs'“header:Header;”类型的参数不可分配给“RequestOptionsArgs”类型的参数 【发布时间】:2017-11-27 21:23:24 【问题描述】:

尝试在 HTTP.POST/GET 请求 Argument of type 'header:Header;' is not assignable to parameter of type 'RequestOptionsArgs'. Property 'null' is missing in the type 'header:Header;' 中传递标头时出现以下错误

我尝试了很多解决方案,但没有锁定它。

这里是我的代码:

import  Injectable  from '@angular/core';
import  Response, RequestOptions, Headers, Http, URLSearchParams from '@angular/http';
import  Observable, Subject  from 'rxjs/Rx';  
import Company from './companyMaster';
import 'rxjs/add/operator/map';
import "rxjs/Rx";
import 'rxjs/add/operator/toPromise';
import CommonService from '../../common.service';
@Injectable()
export class CompanyMasterService 
private GetListActionUrl: string;
private AddEditActionUrl: string;
public headers = new Headers( 'Content-Type': 'application/json' );
public options = new RequestOptions( headers: this.headers );
commonService: CommonService;
constructor(private _http: Http) 
    this.commonService = new CommonService();
    this.GetListActionUrl = this.commonService.RootURL + 'Company/GetList';
    this.AddEditActionUrl = this.commonService.RootURL + "Company/AddEdit";


private RegenerateData = new Subject<number>();
// Observable string streams
RegenerateData$ = this.RegenerateData.asObservable();

GetList(): Promise<Company[]> 
    return this._http.get(this.GetListActionUrl + "?branchId=" + this.commonService.BranchId)
        .toPromise()
        .then(response => this.extractArray(response))
        .catch();


AddEdit(company: Company): any 
    let headers = new Headers( 'content-type': 'application/json' );
    let body = JSON.stringify(
        'BRANCH_ID': company.BRANCH_ID.toString(),
        'COMPANY_ID': company.COMPANY_ID != null ? company.COMPANY_ID.toString() : null, 'COMPANY_NAME': company.COMPANY_NAME
    );
    let options = new RequestOptions( headers: headers);

   return this._http.post(this.AddEditActionUrl, body, options)
        .toPromise()
        .then(response => this.extractArray(response))
        .catch();


protected extractArray(res: Response, showprogress: boolean = true) 
    let data = res.json();

    return data || [];


private getHeaders() 
    // I included these headers because otherwise FireFox
    // will request text/html instead of application/json
    let headers = new Headers();
    headers.append('Accept', 'application/json');
    return headers;

请参阅上面的代码,我在 AddEdit() 方法中传递了标头,但它显示了上面的错误,我尝试了许多解决方案,例如:

更新包 导入@angular/http 尝试了不同的方式来传递标头 Tried this solution

我附上了在 Visual Studio IDE 中显示错误的屏幕截图。见附件 error screenshot 请尽快回复。

package.json


  "name": "sb-admin-angular4-bootstrap4",
  "version": "1.0.0",
  "license": "MIT",
  "scripts": 
    "ng": "ng",
    "start": "ng serve --ec=true",
    "build": "ng build --prod",
    "gitbuild": "ng build --prod --base='/start-angular/SB-Admin-BS4-Angular-4/master/dist/'",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  ,
  "private": true,
  "dependencies": 
    "@angular/common": "^4.0.0",
    "@angular/compiler": "^4.0.0",
    "@angular/core": "^4.0.0",
    "@angular/forms": "^4.0.0",
    "@angular/http": "^4.0.0",
    "@angular/platform-browser": "^4.0.0",
    "@angular/platform-browser-dynamic": "^4.0.0",
    "@angular/router": "^4.0.0",
    "@ng-bootstrap/ng-bootstrap": "^1.0.0-alpha.25",
    "@ngx-translate/core": "^6.0.1",
    "@ngx-translate/http-loader": "0.0.3",
    "core-js": "^2.4.1",
    "font-awesome": "^4.7.0",
    "ionicons": "^3.0.0",
    "ng2-charts": "^1.5.0",
    "rxjs": "^5.1.0",
    "typing": "^0.1.9",
    "zone.js": "^0.8.4"
  ,
  "devDependencies": 
    "@angular/cli": "^1.1.3",
    "@angular/compiler-cli": "^4.0.0",
    "@types/jasmine": "2.5.38",
    "@types/node": "~6.0.60",
    "codelyzer": "~2.0.0",
    "jasmine-core": "~2.5.2",
    "jasmine-spec-reporter": "~3.2.0",
    "karma": "~1.4.1",
    "karma-chrome-launcher": "~2.0.0",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^0.2.0",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "karma-phantomjs-launcher": "^1.0.4",
    "protractor": "~5.1.0",
    "ts-node": "~2.0.0",
    "tslint": "~4.5.0",
    "typescript": "~2.2.0",
    "webpack": "^3.0.0"
  

【问题讨论】:

对我来说同样的问题...也尝试了这种方法 - ``` setContentTemplate(contentItem: ContentItem, template: ContentTemplate) let options = new RequestOptions(); options.headers = new Headers(); options.headers.append('Content-Type', 'text/uri-list');返回 this.http.put(contentItem._links.template.href, template._links.self.href, options); ``` 试试这个解决方案:***.com/a/48535717/4698380,因为不推荐使用 http,您应该改用 HttpClient。 【参考方案1】:

我终于通过跳过 RequestOptions 对象解决了我的问题。 我使用的是 Angular 4,所以对我来说可能有所不同,但我创建了一个无类型对象并将其传递给 http 方法函数... -

import  Identifiable, RestRepositoryService, HalResponse  from '../spring-data/rest-repository.service';
import  Injectable  from '@angular/core';
import  HttpClient, HttpHeaders  from '@angular/common/http';
import  ContentTemplate  from '../content-template/content-template.service';

export class ContentItem extends HalResponse 
  id: string;
  name: string;
  description?: string;
  fieldValues: any;
  template: any;
  contentType: any;


@Injectable()
export class ContentItemService extends RestRepositoryService<ContentItem>  

  getContentType(contentItem: ContentItem) 
    return this.http.get(contentItem._links.contentType.href);
  

  getContentTemplate(contentItem: ContentItem) 
    return this.http.get(contentItem._links.template.href);
  

  setContentTemplate(contentItem: ContentItem, template: ContentTemplate) 
    const headers = new HttpHeaders('Content-Type': 'text/uri-list');
    return this.http.put(contentItem._links.template.href, template._links.self.href, headers: headers);
  

  constructor(http: HttpClient) 
    super(http, '/api/contentItems');
  


【讨论】:

我不得不逐行查看您的代码,为我解决的问题是将 headers = new Headers() 更改为 headers = new HttpHeaders() @kelrob- 是的,它可以通过将 new Headers() 更改为 new HttpHeaders() 来工作。【参考方案2】:

试试这个方法。这有帮助吗

 options = new RequestOptions();
            options.headers = new Headers();
            options.headers.append('Content-Type', 'application/json');
            options.headers.append('X-Requested-With', 'XMLHttpRequest');

【讨论】:

【参考方案3】:

你的问题在这里:

private getHeaders() 
    // I included these headers because otherwise FireFox
    // will request text/html instead of application/json
    let headers = new Headers();
    headers.append('Accept', 'application/json');
    return headers;

HttpHeadersappend()的实现是:

append(name: string, value: string|string[]): HttpHeaders  
   return this.clone(name, value, op: 'a'); 
  

它返回您的 HttpHeaders 对象的克隆。为了使您的附加效果,您需要将其存储在一个变量中:

headers = headers.append('Accept', 'application/json');

【讨论】:

以上是关于“header:Header;”类型的参数不可分配给“RequestOptionsArgs”类型的参数的主要内容,如果未能解决你的问题,请参考以下文章

构建:'string | 类型的参数1' 不可分配给“字符串”类型的参数

构建:'string | 类型的参数1' 不可分配给“字符串”类型的参数

PhpStorm 警告:参数类型不可分配给参数类型

JWT:'string | 类型的参数undefined' 不可分配给'Secret' 类型的参数

类型参数 static: boolean; 不可分配给 read?: any 类型的参数

如何解决“null”类型的参数不可分配给“GoogleUser |”类型的参数不明确的'