2015-07-21 126 views
0

將Laravel和Vue.js合併到一個表中會導致一些問題。Vue.js和Laravel集成問題

本質上,我試圖將v-repeat屬性與使用vue-resources擴展的http:get請求組合使用。問題是沒有任何值可以通過Vue傳遞 - 我只需將括號中的{{first_name}}和{{email_address}}。

我可以確認由http:get請求調用的API方法實際上是在分發數據(手動訪問瀏覽器中的URL顯示數據)。

這裏是在routes.php文件檔案,負責輸出數據的代碼:

get('api/v1_users',function() 
{ 
    return App\User::all()->toJson(); 
}); 

,這裏是它在瀏覽器中吐出:

[{"email_address":"[email protected],"first_name":"John","password":"test123"}] 

的Chrome瀏覽器控制檯不顯示錯誤或警告。

這裏是我的刀片文件:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --> 
    <meta name="description" content=""> 
    <meta name="author" content=""> 
    <link rel="icon" href="../../favicon.ico"> 

    <title>Navbar Template for Bootstrap</title> 

    <!-- Bootstrap core CSS --> 
    <link media="all" type="text/css" rel="stylesheet" href="{{ URL::asset('css/bootstrap.min.css') }}"> 

    <!-- Custom styles for this template --> 
    {!! Html::style('css/navbar.css') !!} 


</head> 

<body> 

<div class="container"> 

    <!-- Static navbar --> 
    <nav class="navbar navbar-default"> 
     <div class="container-fluid"> 
      <div class="navbar-header"> 
       <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar"> 
        <span class="sr-only">Toggle navigation</span> 
        <span class="icon-bar"></span> 
        <span class="icon-bar"></span> 
        <span class="icon-bar"></span> 
       </button> 
       <a class="navbar-brand" href="#">User Password Operations</a> 
      </div> 
      <div id="navbar" class="navbar-collapse collapse"> 
       <ul class="nav navbar-nav"> 
        <li class="inactive"><a href="#">Reset New User</a></li> 
        <li class="inactive"><a href="#">Pending Users</a></li> 
       </ul> 
      </div><!--/.nav-collapse --> 
     </div><!--/.container-fluid --> 
    </nav> 

    <!-- Main component for a primary marketing message or call to action --> 
    <div class="jumbotron"> 
     <h1>Pending 1.0 Users</h1> 
     <p>A list of 1.0 users that have a change!me password as a result of this tool, and are awaiting password change.</p> 
    </div> 
    <table class="table table-bordered"> 
     <tr> 
      <td> 
       <b>Name</b> 
      </td> 
      <td> 
       <b>Email</b> 
      </td> 
      <td> 
       <b>Select</b> 
      </td> 
     </tr> 
     <div id = "user"> 
      <tr v-repeat = "user: v1_user"> 
       <td> 
        @{{ first_name }} 
       </td> 
       <td> 
        @{{ email_address }} 
       </td> 
       <td> 
        <button type="button" class="btn btn-success">Revert Password To Original</button> 
       </td> 
      </tr> 
     </div> 
    </table> 
    <div class="jumbotron"> 
     <h1>Pending 2.0 Users</h1> 
     <p>A list of 2.0 users that have a change!me password as a result of this tool, and are awaiting password change.</p> 
    </div> 
    <table class="table table-bordered"> 
     <tr> 
      <td> 
       <b>Name</b> 
      </td> 
      <td> 
       <b>Email</b> 
      </td> 
      <td> 
       <b>Select</b> 
      </td> 
     </tr> 
     <div> 
      <tr v-repeat = "user: v1_user"> 
       <td> 
        @{{user.first_name}} 
       </td> 
       <td> 
        @{{user.email_address}} 
       </td> 
       <td> 
        <button type="button" class="btn btn-success" v-on= "click: onClick">Revert Password To Original</button> 
       </td> 
      </tr> 
     </div> 
    </table> 
</div> <!-- /container --> 
<!-- Bootstrap core JavaScript 
================================================== --> 
<!-- Placed at the end of the document so the pages load faster --> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
<!-- Vue.js file REP --> 
<script src="/js/vue.js"></script> 
<script src="/js/vue-resource.min.js"></script> 
<!-- Main Vue file--> 
<script src="/js/main.js"></script> 

</body> 
</html> 

這裏是伴隨的JavaScript與Vue.js代碼文件:(main.js)

new Vue({ 
    el: "#user", 

    data: 
    { 
     v1_user:[], 
    }, 

    ready : function() 
    { 
     this.fetchV1IntermediaryUsers(); 
    }, 

    methods: 
    { 
     fetchV1IntermediaryUsers: function() { 
      this.$http.get('/api/v1_users',function(v1users) { 
       this.$set('v1_user',v1users); 
      }); 

     } 
    } 
}); 
+0

檢查您的瀏覽器控制檯,併發布是否有任何錯誤。 – chanafdo

+0

添加了上面的截圖! – mdobrenko

+1

你有兩個不同的部分,使用相同的id'user'。一節不會受Vue影響。用不同的ID或其他方法嘗試不同的實例。你是否正確地獲得一個部分或者沒有一個。 – chanafdo

回答

3

您有多個DIV的具有相同ID的。 HTML中的ID必須是唯一的。當你啓動一個VUE實例時,你將它綁定到一個元素上,在這種情況下,它在你的代碼中兩次。刪除ID並添加一個ID到<Body>標籤,然後檢查您的代碼。

+0

謝謝!這讓我非常沮喪! – mdobrenko