简体   繁体   English

Vuejs 2.4.4 + Laravel 5.5渲染来自api调用的响应

[英]Vuejs 2.4.4 + Laravel 5.5 Render response from api call

I'm having some trouble with rendering data from a api call with vuejs 2.4.4. 我在使用vuejs 2.4.4从api调用呈现数据时遇到了一些麻烦。 and laravel 5.4. 和laravel 5.4。 Im using axios to make the call and get the response. 我使用axios拨打电话并获得响应。

This is the code i'm using 这是我正在使用的代码

<script>
  export default {
    data () {
      return {
        events: {
          loc: []
        },
        eventitem: []
      }
    },

    mounted() {
      this.fetchEventlist();
    },
    methods: {
      fetchEventlist() {
        axios.get('/api/eventslist').then((response) => {
          this.events = response.data;
        });
      }
    }
  }
</script>

To render it to my template this is the code i'm using: 要将其渲染到我的模板,这是我正在使用的代码:

<div class="event-block" v-for="eventitem in events">
        <div class="row">
          <div class="col-xl-7 event-equal-height">
            <div class="media align-items-center">
              <img class="d-flex mr-3" src="/img/events/medevent-temp-100x100.png">
              <div class="media-body">
                <h4>{{ eventitem.eventname }}</h4>
                <p>
                  {{ eventitem.eventdate }}, {{ eventitem.eventstarttime }} - {{ eventitem.eventendtime }}<br>
                  {{ eventitem.loc }}
                </p>
              </div>
            </div>
          </div>
        </div>
      </div>

eventitem.loc is a object, it's a relation in laravel. eventitem.loc是一个对象,它是laravel中的一个关系。 In the view im getting the object json response from .loc. 在视图中,我从.loc获取对象json响应。

The laravel response with relationship: 与关系的幼虫反应:

$events = Event::where('softdelete', 0)->whereDate('eventdate', '>', Carbon::now())->orderBy('eventdate', 'asc')->with('loc')->get();

      return response()->json($events);

I want to get the data from the .loc object, i tried to use eventitem.loc.name etc... but getting errors that the object is 0. What am i doing wrong here? 我想从.loc对象获取数据,我尝试使用eventitem.loc.name等...,但是得到该对象为0的错误。我在这里做错了什么?

Thanks in advance! 提前致谢!

---- Update ----更新

When im doing {{ eventitem.loc }} im getting the object data: 当我做{{eventitem.loc}}时,我正在获取对象数据:

{ "id": 2, "name": "Paradiso", "adress": "Weteringschans 6-8", "zipcode": "1017 SG", "city": "Amsterdam", "country": "Nederland", "maplong": "4.8838064999999915", "maplat": "52.3621516", "created_at": "-0001-11-30 00:00:00", "updated_at": "2017-09-12 09:13:54" }

When i do {{ eventitem.loc.name }} Im getting the error: 当我做{{eventitem.loc.name}}时,我收到错误消息:

[Vue warn]: Error in render function: "TypeError: Cannot read property 'name' of undefined"

--- update 2 Don't know if its the correct way but the following code did make it work. --- update 2不知道它是否正确,但是下面的代码确实使它起作用。

<span v-if="eventitem.loc">
   {{ eventitem.loc.name }}
</span>
<span v-if="eventitem.loc">
   {{ eventitem.loc.name }}
</span>

this did fix the problem... :) 这确实解决了问题... :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM