2016-11-27 20 views
3

所以我使用Laravel 5.3,我試圖創建一個DataTable,因此,我試圖創建一個方法,從後端獲取數據,我試圖在組件準備好後立即調用它。方法沒有定義在掛鉤,Vue JS

我發現ready()鉤子現在已經死了,換成了mounted(),因此我的模板看起來像這樣。

<template> 
..Simple Bootstrap table... 
</template> 

<script> 
    export default { 
     data:() => { 
      return { 
       searchQuery: "", 
       columns: ['ID', 'Name', 'Campaign', 'Method', 'Limit Per Connection', 'Limit Per Day', 'Active', 'Last Ran'], 
       lastId: 0, 
       rowsPerPage: 10, 
       gridData: [ 
        { id: 1, name: "Example 1", campaign: 3412, method: "POST", limitConn: 1250, limitDay: 50, active: "Yes", lastRun: "2016-11-27 18:42:21"}, 
        { id: 2, name: "Example 1", campaign: 3412, method: "POST", limitConn: 1250, limitDay: 50, active: "Yes", lastRun: "2016-11-27 18:42:21"}, 
        { id: 3, name: "Example 1", campaign: 3412, method: "POST", limitConn: 1250, limitDay: 50, active: "Yes", lastRun: "2016-11-27 18:42:21"}, 
        { id: 4, name: "Example 1", campaign: 3412, method: "POST", limitConn: 1250, limitDay: 50, active: "Yes", lastRun: "2016-11-27 18:42:21"}, 
        { id: 5, name: "Example 1", campaign: 3412, method: "POST", limitConn: 1250, limitDay: 50, active: "Yes", lastRun: "2016-11-27 18:42:21"}, 
        { id: 6, name: "Example 1", campaign: 3412, method: "POST", limitConn: 1250, limitDay: 50, active: "Yes", lastRun: "2016-11-27 18:42:21"}, 
        { id: 7, name: "Example 1", campaign: 3412, method: "POST", limitConn: 1250, limitDay: 50, active: "Yes", lastRun: "2016-11-27 18:42:21"} 
       ] 
      } 
     }, 
     methods: { 
      /** 
      * Fetch JSON data for crons from the Backend 
      * 
      * @param lastId - The last ID in the current data 
      */ 
      fetchData: (lastId) => { 
       Vue.http.get('/data').success((response) => { 

        console.log(response); 

       }).error((response) => { 

        console.error(response); 

       }) 
      }, 
     }, 
     mounted:() => { 
      // When the Component is ready fetch the JSON from the Server Backend 
      this.fetchData(0); 
     }, 
    } 
</script> 

<style>...My Css...</style> 

的安裝方法火災,但說this$1.fetchData is not defined任何知道我做錯了嗎?懸掛掛鉤是否無法訪問我的方法?

回答

10

語法mounted應該像下面:

mounted() { 
    // When the Component is ready fetch the JSON from the Server Backend 
    this.fetchData(0); 
} 

Don't use arrow function for lifecycle hooks,箭頭功能使用由他們的背景和VUE確定詞彙this將無法​​綁定它給我們。

+0

哦,哇,就是這樣,到底是什麼,是不是隻是定義功能?這樣做或使用ES6語法究竟有什麼區別?非常感謝 :) – JonnySerra