简体   繁体   English

找不到名称'标题'。 在角2

[英]Cannot find name 'headers'. in angular 2

enter image description here I am working on angular 2.this is my ts file I called getRandomQuote() method in constructor. 在这里输入图像描述我正在处理角度2.这是我的ts文件我在构造函数中调用了getRandomQuote()方法。 But when i run the project i get below error:- 但是,当我运行该项目时,我得到以下错误: -
Cannot find name 'headers'. 找不到名称'标题'。 Did you mean the instance member 'this.headers'? 你的意思是实例成员'this.headers'吗?

import {Component, ViewEncapsulation, Injectable} from '@angular/core';
import {PaginatePipe, PaginationControlsCmp, PaginationService} from 'ng2-pagination';
import { Http, Response, Headers,RequestOptions } from 'angular2/http';
import {BasicTablesService} from './basicTables.service';
import {BaCard} from '../../../../theme/components';
import {HoverTable} from './components/hoverTable';
import {BorderedTable} from './components/borderedTable';
import {CondensedTable} from './components/condensedTable';
import {StripedTable} from './components/stripedTable';
import {ContextualTable} from './components/contextualTable';
import {ResponsiveTable} from './components/responsiveTable';

import {AuthHttp, AuthConfig, AUTH_PROVIDERS } from 'angular2-jwt';
import {HTTP_BINDINGS} from 'angular2/http';



@Component({
  selector: 'basic-tables',
  viewProviders: [PaginationService],
  pipes: [PaginatePipe, ResponsiveTable, ContextualTable],

  encapsulation: ViewEncapsulation.None,
  directives: [BaCard, HoverTable, BorderedTable, CondensedTable, StripedTable, ContextualTable, ResponsiveTable, PaginationControlsCmp,HTTP_BINDINGS],
  styles: [require('./basicTables.scss')],
  template:  `

<todo-search></todo-search>

<table class="table table-hover">

<div >Enter ID: <input type="text" #listFilter (keyup)="0" style="color:black"  /></div> <div>Alert on click<button (click)="clicked()" style="color:black">Click</button></div>
<span>{{ test }}</span>
    <tr class="black-muted-bg">

      <th class="align-left">ID</th>
      <th class="align-left">Name</th>
      <th class="align-left">Protocol</th>
      <th class="align-left">Inbound Business Process</th>
      <th class="align-left">Outbound Business Process</th>

    </tr>
        <tbody>
          <tr *ngFor="let item of randomQuote  | paginate: { itemsPerPage: 20, currentPage: p } | ResponsiveTable:listFilter.value ">
        <td>{{item.connectionId}}</td>
       <td>{{item.name}}</td>
        <td>{{item.protocol}}</td>
        <td>{{item.inBoundBPName}}</td>
         <td>{{item.outBoundBPName}}</td>


    </tr>
</tbody>
        <pagination-controls (pageChange)="p = $event" #api></pagination-controls>
    </table>

  `,
  providers: [BasicTablesService]
})
export class BasicTables {




    public body = JSON.stringify(
    {
    "startIndex": 0,
    "numofIds": 15,
    "programId": null,
    "emailId":"admin@justransform.com",
    "searchStr":"",
    "transactionId":"",
    "status":"",
    "srcConnectionName":"",
    "destConnectionName":"",
        "inBoundBPName":"",
        "outBoundBPName":"",
        "fileContent":""
     }

);

    public headers = new Headers({ 'Content-Type': 'application/json' });
    public options = new RequestOptions({ headers: headers }); 
    private url = 'http://uat.justransform.com:8080/justransform/transaction/find?sortByColumn=transactionId&sortByOrder=Desc';




  randomQuote:Array<any> = [];
getRandomQuote() {
  this.http.post(this.url, this.body, this.options)
    .map((res:Response) => res.json())
    .subscribe(  
      data => {this.randomQuote = data},
      err => this.logError(err), 
      () => console.log('Random Quote Complete')

    );

} 

logError(err) {
  console.error('There was an error: ' + err);
}
  clicked(event) {
  alert("Alert");

  }


  constructor(public http: Http) {

  this.getRandomQuote();

  }
}

Your code defines the headers attribute in the class context and tries to access it directly after that using headers . 您的代码在类上下文中定义headers属性,并尝试在使用headers后直接访问它。

public headers = new Headers({ 'Content-Type': 'application/json' });
public options = new RequestOptions({ headers: headers }); 

The error message you get for that specifically tells you what to try: 您获得的错误消息明确告诉您要尝试的内容:

Cannot find name 'headers'. 找不到名称'标题'。 Did you mean the instance member 'this.headers'? 你的意思是实例成员'this.headers'吗?

This is because you defined headers in the class context. 这是因为您在类上下文中定义了headers To properly access it, you have to use this.headers : 要正确访问它,您必须使用this.headers

public options = new RequestOptions({ headers: this.headers });
//                                             ^ here

See TypeScript Classes for more information. 有关更多信息,请参见TypeScript类

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

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