2017-02-10 44 views
0

我正在使用新Laravel 5.4的Vue模板。在我的Office.vue我有這個代碼。Laravel 5.4和Vue 2.0.1無法讀取未定義的屬性'get'

<template> 
    <div class="container"> 
     <div class="row"> 
      <div class="col-md-8 col-md-offset-2"> 
       <div class="panel panel-default"> 
        <div class="panel-heading">Example Component</div> 

        <div class="panel-body"> 
         I'm an example component! {{ message}} | {{ type}} | {{ number }} 
        </div> 
        <span id="vue"></span> 
       </div> 
      </div> 
     </div> 
    </div> 
</template> 

<script> 
    export default { 
     mounted() { 
      return this.displayOffices(); 
     }, 
     data: function(){ 
      return { 
       message: "aw", 
       type: "test", 
       number: 2 
      } 
     }, 
     methods: { 
      displayOffices: function(){ 
       this.$http.get('/vueOffice', function(course) { 
        console.log(course); 
       }); 
      } 
     } 
    } 
</script> 

顯示的數據是好的,很好的工作,但我得到的錯誤與

Uncaught TypeError: Cannot read property 'get' of undefined

這是此。$ http.get

我試圖做到的是從返回JSON數據的路線獲取數據。

我需要幫助解決這個錯誤,一直試圖尋找答案,但每個人都用不同的方式來實現Vuejs。

+1

如果您正在使用'axios'而不是'vue-resource'然後r使用'axios'放置'this。$ http'。 – mrabbani

+0

您好我可以標記該所接受,感謝 – Ikong

回答

1

如果您正在使用的axios代替vue-resource然後用axios替換this.$http

1

您必須安裝vue-resource可以使用this.$http.get

,或者你可以安裝axios和使用axios.get代替this.$http.get

+0

我有安裝VUE資源,並重新建立,但仍存在問題,我用愛可信現在,它的工作的偉大 – Ikong

0

不推薦使用此之前,$ http.get爲Vue的希望遠離支持它。他們建議使用使用axios的庫。然後您可以使用this.axios.get()

self.axios 
    .get(apiUrl, { 
    params: this.formData, 
    }) 
    .then((response) => { 

    }); 
0

另一種方法是將以下代碼添加到您的文件:

Vue.prototype.$http = axios; 
+0

不,不這樣做。你爲什麼?這是非常糟糕的做法 –

0

添加此require('vue-resource');在文件中


/resources/assets/js/bootstrap.js

後:

window.Vue = require('vue');

相關問題