javascript - Vue.js:v-repeat 未从 JSON 数组获取数据

标签 javascript vue.js

我有一个 JSON 项目数组,我尝试使用 v-repeat 将其输出为一系列表行:

<tr v-repeat="items">
    <td>{{code}} </td>
    <td> {{description}}</td>
    <td>
        {{price}}
    </td>
    <td><input v-model="quantity" type="text" size="4"/></td>
    <td> {{total = price * quantity}}</td>
</tr>

JSON 中的每个项目都会输出一行带有空单元格的行 - 因此 Vue.js 没有获取属性值,即使我已经确认它们确实存在并且我可以在 v-repeat 中获取值我做了这样的事情:

{{items[$index].code}}

我没有收到任何错误或警告。

这是我的 Vue 数据对象的 JSON 输出:

{ "items":[{
    "id": "408",
    "product_id": "6",
    "description": " item description here... ",
    "price": "1210.26",
    "created_at": "2015-06-04 15:10:14",
    "updated_at": "2015-06-04 15:10:14",
    "quote_id": "32",
    "quantity": "1",
    "code": "PI0055"
}]}

最佳答案

你的代码对我来说工作得很好:

var data = {
    "items":[{
        "id": "408",
        "product_id": "6",
        "description": " item description here... ",
        "price": "1210.26",
        "created_at": "2015-06-04 15:10:14",
        "updated_at": "2015-06-04 15:10:14",
        "quote_id": "32",
        "quantity": "1",
        "code": "PI0055"
    }]
};

var vue = new Vue({
    el: '#tbl',
    data: data
});
<table id="tbl" border="1">
    <tr v-repeat="items">
        <td>{{code}} </td>
        <td>{{description}}</td>
        <td>{{price}}</td>
        <td><input v-model="quantity" type="text" size="4"/></td>
        <td>{{total = price * quantity}}</td>
    </tr>
</table>
<script src="http://vuejs.org/js/vue.min.js"></script>

关于javascript - Vue.js:v-repeat 未从 JSON 数组获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30702881/

相关文章:

javascript - 如何修复 Gatsby 开发错误 : Cannot find module 'gatsby-cli/lib/reporter' ?

javascript - 默认选中单选按钮 - JQuery 事件不起作用

javascript - dayjs() 告诉我这个日期字符串的日期无效

javascript - Vue.js:无法在 v-for 中排序

vue.js - 如何在 Vue 中使用 VeeValidate 时修复 "TypeError: plugin is undefined"错误

javascript - JQuery 通过类名获取所有元素

javascript - 无法调用ajax函数

javascript - 如何使以下 Vue 代码具有反应性?

JavaScript SRC 路径不起作用

vue.js - 重新加载时Vue路由器重定向到主页