Vue.js 和 Laravel 集成问题

Vue.js and Laravel integration issue

运行 结合 Laravel 和 Vue.js 来填充 table.

有点问题

本质上,我试图将 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":"test123@gmail.com,"first_name":"John","password":"test123"}]

Chrome 控制台不显示任何错误或警告。

这是我的 blade 文件:

<!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>

这是带有 Vue.js 代码的附带 javascript 文件:(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);
            });

        }
    }
});

您有多个 DIV 具有相同的 ID。 HTML 中的 ID 必须是唯一的。当你启动一个 VUE 实例时,你将它绑定到一个元素,在这种情况下,它在你的代码中出现了两次。删除 ID 并将 ID 添加到 <Body> 标记,然后检查您的代码。