2016-07-12 166 views
1

enter image description here 我對角2,本工作是我的TS文件我叫getRandomQuote()的構造函數方法。 但是當我運行該項目,我得到以下錯誤: -
無法找到名爲「頭」。你是不是指實例成員'this.headers'?找不到名稱'標題'。在角2

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":"[email protected]", 
    "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(); 

    } 
} 
+1

請張貼代碼在你的問題中,而不是文本的圖像。 – SurvivalMachine

+0

Thanx,我添加了文本格式的完整代碼 – Swagat

+1

您是否閱讀過錯誤消息? – str

回答

3

你的代碼定義在類上下文headers屬性,嘗試直接,使用headers後訪問它。

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

你得到該錯誤消息明確告訴你去嘗試什麼:

Cannot find name 'headers'. Did you mean the instance member 'this.headers'?

這是因爲你在類上下文中定義headers。要正確地訪問它,你必須使用this.headers

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

更多信息請參見TypeScript Classes

+0

是的,我也嘗試着用這個,但如果我用這個扔了以下錯誤: - 意外的指導價值「的翻譯:」在組件 – Swagat

+0

它是指哪條線的觀? – str

+0

我解決這個問題,但解決這個控制檯上我收到此錯誤後:在BasicTables發現-Directive註釋 – Swagat