简体   繁体   English

Vue JS V-for 不渲染

[英]Vue JS V-for not rendering

Apologies for the beginner question.为初学者问题道歉。 I'm a beginner to Vue and have been struggling to understand why my v-for is not rendering any data.我是 Vue 的初学者,一直在努力理解为什么我的v-for没有呈现任何数据。 I can see the array in the Vue console in data , but, the table does not render.我可以在 Vue 控制台中的data看到数组,但是,该表没有呈现。

See below for my code:请参阅下面的我的代码:

HTML: HTML:

<div id="app">
  <div class="container">
    <table class="table">
      <thead class="t-head-light">
        <tr>
            <th>Description</th>
            <th>Year 1</th>
            <th>Year 2</th>
            <th>Year 3</th>
            <th>Year 4</th>
            <th>Year 5</th>
        </tr>
    </thead>
    <tbody>
      <tr v-for="item in transactions">
        <td>{{item.account_category}}</td>
      </tr>
    </tbody>
    </table>
  </div>
</div>

Vue JS: VueJS:

var vm = new Vue({
  el: '#app',
  data: {
    transactions: []
  },
  mounted: function(){
      this.fetchTransactions()
  },
  methods:{
    fetchTransactions: function(){
      var url = 'http://127.0.0.1:8000/scenarios/transactions'
      fetch(url)
        .then((resp) => resp.json())
        .then(function(data){
        vm.transactions = data;
      })
    }
  }
})

Just want to know what I'm doing wrong.只是想知道我做错了什么。


EDIT:编辑:

Thanks for the responses, my transactions data looks like this:感谢您的回复,我的交易数据如下所示:

Vue控制台中的数据输出

I will look into computed transactions too.我也会研究computed交易。

EDIT 2: Thanks for the response, I figured out that it was the curly braces I used in the HTML.编辑 2:感谢您的回复,我发现这是我在 HTML 中使用的花括号。 Since I'm using Django as the back-end, it got confused between jinja and vue I think.由于我使用 Django 作为后端,所以我认为它在 jinja 和 vue 之间混淆了。 I've since changed Vue's default delimiter to square brackets and it now works.我已经将 Vue 的默认分隔符更改为方括号,现在可以使用了。

In Vue, it is mandatory that you give a key while applying v-for在 Vue 中,你必须在应用v-for给出一个 key

HTML HTML

<div id="app">
  <div class="container">
    <table class="table">
      <thead class="t-head-light">
        <tr>
            <th>Description</th>
            <th>Year 1</th>
            <th>Year 2</th>
            <th>Year 3</th>
            <th>Year 4</th>
            <th>Year 5</th>
        </tr>
    </thead>
    <tbody>
      <tr v-for="(item, i) in transactions" :key="i">
        <td>{{item.account_category}}</td>
      </tr>
    </tbody>
    </table>
  </div>
</div>

VueJs VueJS

methods:{
    fetchTransactions: function(){
      var url = 'http://127.0.0.1:8000/scenarios/transactions'
      fetch(url)
        .then((resp) => resp.json())
        .then(function(data){
        this.transactions = data;
      })
    }
  }

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

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