【问题标题】:V-for loop in vuejs doesn't return anythingvuejs 中的 V-for 循环不返回任何内容
【发布时间】:2018-06-15 04:27:22
【问题描述】:

我正在 laravel 中尝试这个 vuejs 应用程序,它是一个简单的应用程序,我在其中获取数据,然后尝试用 laravel 显示它,这是我的 messages.vue

Messages.vue

<template lang="html">
<div id="frame2">
  <div  id="contacts">
    <ul>
      <div v-for="privsteMsg in privsteMsgs">
        <li class="contact">
          <div class="wrap">
            <span class="contact-status online"></span>
            <img :src="'/uploads/imgs/avatars/' + privsteMsg.avatar">
            <div class="meta">
              <p class="name">{{privsteMsg.name}}</p>
              <p class="preview">Hey it's me</p>
            </div>
          </div>
        </li>
      </div>
    </ul>
  </div>

</div>

</template>
<script>
export default {
  props: ['userid'],
  data() {
    return {
      privsteMsgs: [],
      bUrl1: 'http://test.app:8000',
    }
  },

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

  created(){
    axios.get(this.bUrl1 + '/api/getMessages')
      .then(response => {
        app.privsteMsgs = response.data; //we are putting data into our posts array
        console.log(app.privsteMsgs);
      })
      .catch(function (error) {
        console.log(error); // run if we have error
      });

  },

}
</script>

奇怪的是console.log(app.privsteMsgs); 正常返回数据,但是当我尝试循环遍历它时,没有显示好像没有数据。

【问题讨论】:

  • 你的app变量是什么?
  • 对不起,什么应用变量?
  • app.privsteMsgs。你如何定义app?应该是this.privsteMsgs=...
  • 在您的代码中,您像 app.privateMsgs 一样执行此操作,但您的 app 变量声明在哪里?
  • 那太尴尬了,自从我更改了文件后就没有更改过,谢谢大家

标签: laravel vue.js vuejs2


【解决方案1】:

您已声明 app 变量但未定义。用这个替换 app 变量,例如

created(){
        let _this = this
        axios.get(this.bUrl1 + '/api/getMessages')
          .then(response => {
            _this.privsteMsgs = response.data; //we are putting data into our posts array
            console.log(_this.privsteMsgs);
          })
          .catch(function (error) {
            console.log(error); // run if we have error
          });

      }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-01
    • 1970-01-01
    • 2018-08-04
    • 2020-09-25
    • 1970-01-01
    • 1970-01-01
    • 2018-10-24
    • 1970-01-01
    相关资源
    最近更新 更多