2017-08-03 90 views
1

您好我只想使用一個簡單的函數,http post來發布我的日期到一個頁面。我希望服務器能夠獲取我發佈的日期。TypeError:無法讀取未定義的屬性'post'

import { Component, OnInit } from '@angular/core'; 
import { MarginServcies } from '../../services/MarginService'; 
import { Response } from '@angular/http'; 
import { Http} from '@angular/http'; 
//import { Ng2SmartTableModule, LocalDataSource } from 'ng2-smart-table'; 


@Component({ 
    selector: 'margin', 
    template: require('./margin.component.html') 
}) 


export class MarginComponent implements OnInit { 
    public http: Http; 
    public MarginList = []; 
    public date: string; 
    public userdate: string; 
    pad(i: number) { 
     return i < 10 ? '0' + i : i; 
    } 
    onClick($event: any, userdate) { 
     userdate = this.date; 
     //console.log(this.date); 
     this.http.post('api/date', userdate).subscribe(); 
    } 

    //get the current date as default 
    ngOnInit() { 
     const selectedDate = new Date(); 
     this.date = `${selectedDate.getFullYear()}-${this.pad(selectedDate.getMonth() + 1)}-${this.pad(selectedDate.getDate())}`; 
    } 

    public constructor(private marginService: MarginServcies) { 

     //get Margindata from server 
     this.marginService.getMargin().subscribe(results => { 
      this.MarginList = results.json(); 
      //console.log(results.json()); 
     }); 


    } 

} 

這是我的組件。我正在使用webpack,angular 4.我想在onClick()函數中使用post函數。一旦我點擊我可以發佈日期。現在我檢查console.log()工作正常。錯誤是:

MarginComponent.html:15 ERROR TypeError: Cannot read property 'post' of undefined 
    at MarginComponent.onClick (margin.component.ts:25) 
    at Object.eval [as handleEvent] (MarginComponent.html:15) 
    at handleEvent (vendor.js?v=8G5nuphpleAMB8hDVj0v8l9es64guglhqGWIwYdey3M:22850) 
    at callWithDebugContext (vendor.js?v=8G5nuphpleAMB8hDVj0v8l9es64guglhqGWIwYdey3M:24142) 
    at Object.debugHandleEvent [as handleEvent] (vendor.js?v=8G5nuphpleAMB8hDVj0v8l9es64guglhqGWIwYdey3M:23730) 
    at dispatchEvent (vendor.js?v=8G5nuphpleAMB8hDVj0v8l9es64guglhqGWIwYdey3M:19750) 
    at vendor.js?v=8G5nuphpleAMB8hDVj0v8l9es64guglhqGWIwYdey3M:20342 
    at HTMLButtonElement.<anonymous> (vendor.js?v=8G5nuphpleAMB8hDVj0v8l9es64guglhqGWIwYdey3M:27870) 
    at ZoneDelegate.invokeTask (vendor.js?v=8G5nuphpleAMB8hDVj0v8l9es64guglhqGWIwYdey3M:83738) 
    at Object.onInvokeTask (vendor.js?v=8G5nuphpleAMB8hDVj0v8l9es64guglhqGWIwYdey3M:15077) 

任何人都可以幫助我嗎?謝謝!

+0

我建議你閱讀https://angular.io/guide/dependency-injection –

+0

和最佳實踐,建議你換你的HTTP調用的服務。如果你願意,我可以在這裏粘貼一些代碼。否則我在這裏有一個例子:https://github.com/DeborahK/Angular2-GettingStarted – DeborahK

回答

5

只要聲明public http: Http將不會使Http供應商實例在組件內可用。您必須在構造函數中注入http服務,這會帶來http對象。

public constructor(
    private marginService: MarginServcies, 
    private http: Http //<-- added Http service. 
) { 
//your code as is 
} 

Remove the declaration of public http: Http (http declaration) from start of class public http: Http

+0

@Rachel請檢查這個[pastebin](https://pastebin.com/XFyPMm1n)我已經把所有的組件代碼都集成了 –

0

HTTP是從角HTTP模塊

import { Http, Response } from '@angular/http'; 

所以,你必須在到構造注入該HTTP服務的內置角提供商。那麼只角框架解決的依賴

export class YourService { 
    constructor(private http: Http) { 

    } 
相關問題