2017-09-06 23 views
0

(我編輯我的代碼,它的工作!)laravel愛可信vue.js /錯誤連接到數據庫

我試圖讓我的數據庫對象與Vue公司和Laravel使用它們。

我data.blade.php:slight_smile:

<!DOCTYPE html> 
<html> 
<head> 
    <title> Tab test</title> 
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.5.1/css/bulma.css"> 
</head> 

<body> 
    <div id="app"> 
     <users></users> 
    </div> 
</body> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.js"></script> 

<script src="https://unpkg.com/axios/dist/axios.min.js"></script> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script> 
<script src="/js/customers.js"></script> 
</html> 

我customers.js

Vue.component('users',{ 
    template : ` 
     <table class="users"> 
      <thead> 
       <tr> 
        <th>#</th> 
        <th>Name</th> 
        <th>Email</th> 
        <th>Creation Data</th> 
       </tr> 
      </thead> 
      <tbody> 
       <tr v-for="user in users"> 
        <th>{{ user.id }}</th> 
        <th>{{ user.lastname }}</th> 
        <th>{{ user.email }}</th> 
        <th>{{ user.created_at }}</th> 
       </tr> 
      </tbody> 
     </table> 
    `, 
    data: function() { 
     return { 
      users: [] 
     } 
    }, 
    created: function() { 
     this.getUsers(); 
    }, 
      methods: { 
        getUsers: function(){ 


         axios.get("/apps").then((response) => {this.apps = response.data}) 
    } 
        } 

}), 
new Vue({ 
    el: '#app', 


}); 

而且我api.php

Route::get('/users', function(){ 
    return App\User::all(); 
})->name('api_users'); 

我的錯誤是:

enter image description here

你有什麼想法嗎?我認爲這是axios的問題,但我不知道如何解決它。 ?

感謝您的任何意見或解決方案:)

+1

'axios.get (「{{route('api_users')}}」)'是你的問題。你需要傳遞你想要'axios'的實際url來發出請求到 – thanksd

回答

0

{{route('api_users') }}是葉片功能,不能在你的.js文件中使用。

你可以:

  • 硬編碼的URL /users在JavaScript文件
  • 結合的路線,在刀片和訪問前端它通過有
+0

感謝您的回覆如此之快!像那樣:axios.get(「/ customers/list/data」) .then(response => {this.users = response.data.users}) }? – Virtualbees

+0

根據您的路線'api_users'是'/ users'而不是'/ customers/list/data',但這是一般的想法是的 – Quezler

+0

對不起,我是vue.js中的新人,我不明白如何做到這一點.... – Virtualbees