VueJS访问JSON数据

时间:2017-02-05 14:26:12

标签: json laravel vue.js vuejs2

我通过我希望在列表中显示的API访问数据。我现在正在测试VueJS并且无法正确显示它。

数据是:

{

  "data": [
    {
      "userReference": "R100",
      "responderStatus": 0,
      "location": {
        "latitude": 100,
        "longitude": 100,
        "distance": null
      },
      "featureTypeId": 1
    }
  ],
  "pagination": {
    "total": 1,
    "perPage": 10,
    "lastPage": 1,
    "nextPageUrl": null,
    "prevPageUrl": null,
    "from": 1,
    "to": 1,
    "totalPages": 1,
    "currentPage": 1,
    "hasMorePages": false
  }
}

HTML:

<div class="wrapper wrapper-content animated fadeInRight">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="text-center m-t-lg">
                            <h1>MEDIFAKTOR server</h1>
                        </div>

                        <div id="app">
                            <ul class="list-group">
                                <li v-for="responder in responders">@{{ responder[0] }}</li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
    </div>

VueJS:

var app = new Vue({
    el: '#app',
    data: {
        responders: []
    },
    mounted: function() {
        $.get('/api/v1/responders', function(data) {
            app.responders = data;
        })

我只想显示&#34; userReference&#34;每个列表项目。

1 个答案:

答案 0 :(得分:4)

您必须遍历responders.data,因为它是您必须循环的数组,而您只需访问userReference,例如:{{responder.userReference }}。你应该能够像下面这样做:

<div id="app">
    <ul class="list-group">
        <li v-for="responder in responders.data">{{ responder.userReference }}</li>
    </ul>
</div>