简体   繁体   English

Vue v-for 循环“项目”未定义

[英]Vue v-for loop 'item' is Undefined

I'm having an issue where "item" is being returned as undefined when trying to loop over the contents and display it in my HTML.我遇到了一个问题,即在尝试遍历内容并将其显示在我的 HTML 中时,“项目”被返回为未定义。 Any help is greatly appreciated.任何帮助是极大的赞赏。

jinja2.exceptions.UndefinedError: 'item' is undefined jinja2.exceptions.UndefinedError: 'item' 未定义

HTML HTML

<template id="home">
    <div>
        <b-button v-b-toggle.collapse-1 variant="primary">Test</b-button>
        <b-collapse id="collapse-1" class="mt-2">
          <b-card>
            <p class="card-text" v-for="item in records">
                {{ item.model }}
            </p>
          </b-card>
        </b-collapse>
    </div>
</template>

JAVASCRIPT爪哇脚本

const home = {
    template: '#home',
    data: function () {
        return {
            records: [],
            loading: true
        }
    },
    created: function () {
        this.loadRecords();
    },
    computed: {
    },
    methods: {
        async loadRecords() {
            this.loading = true;

            try {
                await axios.request({
                    url: 'load_records',
                    method: 'GET'
                })
                .then(response => {
                    this.records = JSON.stringify(response.data['records']);
                    console.log(this.records);
                })
            } catch (err) {
                alert("Could not get the list of devices\n" + err);
            }
        },
    }
}

console.log(this.records);

[{"aaa_binding":"F","aaa_connection":"T","aaa_console":"T","aaa_network":"T","aaa_system":"F","aaa_vty":"T","banner":"T","exec_timeout_console":"F","exec_timeout_ssh":"T","finger_service":"T","hostname":"test1","id":615,"logging_buffer":"T","logging_remote":"T","logging_timestamp":"T","loopback":"T","model":"Test-model1","ntp":"T","ntp_binding":"T","remote_startup_config":"T","snmp":"F","vendor":"Cisco"},{"aaa_binding":"F","aaa_connection":"T","aaa_console":"T","aaa_network":"T","aaa_system":"F","aaa_vty":"T","banner":"T","exec_timeout_console":"F","exec_timeout_ssh":"T","finger_service":"T","hostname":"Test2","id":892,"logging_buffer":"T","logging_remote":"T","logging_timestamp":"T","loopback":"T","model":"Test-Model2","ntp":"T","ntp_binding":"T","remote_startup_config":"T","snmp":"F","vendor":"Cisco"}]

I will recommended one advice, add v-bind:key="" to the directive v-for:我将推荐一个建议,将 v-bind:key="" 添加到指令 v-for:

<p class="card-text" v-for="(item, index) in records" v-bind:key="index">
  {{ item.model }}
</p>

In addittion, make sure that records array has 'model' property.此外,请确保记录数组具有 'model' 属性。

Change this code:更改此代码:

this.records = JSON.stringify(response.data['records']);

to:到:

this.records = response.data.records;

JSON.stringify() method is converting Object(JSON) type to String type. JSON.stringify()方法将 Object(JSON) 类型转换为 String 类型。

So after call API, this.record's data type is String所以调用API后,this.record的数据类型是String

You need to change code;您需要更改代码;

this.records = response.data.records

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

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