Javascript Vue JS V-用于不渲染
为这个初学者的问题道歉。我是Vue的初学者,一直在努力理解为什么我的Javascript Vue JS V-用于不渲染,javascript,vue.js,Javascript,Vue.js,为这个初学者的问题道歉。我是Vue的初学者,一直在努力理解为什么我的v-for没有呈现任何数据。我可以在Vue控制台中的数据中看到数组,但该表不呈现 请参阅下面的代码: HTML: 我只是想知道我做错了什么 编辑: 感谢您的回复,我的交易数据如下所示: 我还将研究计算的事务 编辑2: 感谢您的回复,我发现这是我在HTML中使用的花括号。因为我使用Django作为后端,所以我认为它混淆了jinja和vue。此后,我将Vue的默认分隔符更改为方括号,现在可以使用了。在Vue中,必须在应用v-fo
v-for
没有呈现任何数据。我可以在Vue控制台中的数据中看到数组,但该表不呈现
请参阅下面的代码:
HTML:
我只是想知道我做错了什么
编辑:
感谢您的回复,我的交易数据如下所示:
我还将研究计算的
事务
编辑2:
感谢您的回复,我发现这是我在HTML中使用的花括号。因为我使用Django作为后端,所以我认为它混淆了jinja和vue。此后,我将Vue的默认分隔符更改为方括号,现在可以使用了。在Vue中,必须在应用v-for
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>
更好地使用Vue.js中的COMPUTED属性。还有一个简单的问题,您肯定可以使用正确的结构获得数据(使用console.log(数据)控制它)
在vm.transactions=data之前;
?并且可能使用this.transactions
而不是vm.transactions
如果您添加data
的输出,我可以向您展示如何使用Vue.js中的COMPUTED属性(因为我无法获取您的事务数据)哦,还有一件事在v-for=“…”之后添加
一个键,如v-for=“交易中的项目”:key=“item”
:对于v-for数据来说,键是必需的
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;
})
}
}
})
<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>
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;
})
}
}