【问题标题】:Vue.js and Laravel integration issueVue.js 和 Laravel 集成问题
【发布时间】:2015-10-10 10:05:18
【问题描述】:

在结合 Laravel 和 Vue.js 来填充表格时遇到了一点问题。

本质上,我试图将 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 控制台不显示任何错误或警告。

这是我的刀片文件:

<!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);
            });

        }
    }
});

【问题讨论】:

  • 检查您的浏览器控制台并发布是否有任何错误。
  • 添加了上面的截图!
  • 您有两个不同的部分具有相同的 ID user。一节不会受到 Vue 的影响。尝试使用不同 id 或其他方法的不同实例。你得到一个部分是正确的还是一个都没有。
  • 嗯,很高兴知道。问题是这两个部分都没有正确出现......我已经尝试过更改 id 和诸如此类的东西,但它似乎没有帮助。看起来 vue 正在根据该警告运行,所以我完全不知道它为什么不工作
  • 虽然,我最后的想法是因为我引用的元素是 v-repeat 元素 (v1_user) 的父元素,但实际数据正在 下吐出标签。这有关系吗? v-repeat 元素的子元素可以在没有任何特殊标签或修饰符的情况下访问数据吗?

标签: laravel vue.js


【解决方案1】:

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

【讨论】:

  • 非常感谢!这让我非常沮丧!
猜你喜欢
  • 2018-04-06
  • 2019-11-25
  • 2020-04-20
  • 2019-02-09
  • 1970-01-01
  • 2019-02-10
  • 1970-01-01
  • 1970-01-01
  • 2021-05-29
相关资源
最近更新 更多