如何使用Vue js通过v-for循环显示动态数据?

时间:2020-04-13 21:13:32

标签: loops vue.js single-page-application

我有一个名为候选表的表,表中有几列。即使一个用户只能有一个配置文件,我还是想知道如何使用v-for循环显示数据,因为我将在应用程序的其他页面中使用它。

当我console.log(response)时,我可以在控制台中看到数据,但未显示。我想念什么?

CandidateProfileIndex.vue:

<template>
    <div>
        <table class="table">
            <thead>
                <tr>
                    <th>Id</th>
                    <th>experience</th>
                    <th>skills</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="(profile, index) in profiles" :key="index">
                    <td>{{profile.id}}</td>
                    <td>{{profile.experience}}</td>
                    <td>{{profile.skills}}</td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script>
    import * as profileService from '../../services/candidate_profile_service.js';

    export default {
        name: "candidateProfileIndex",

        data() {
            return {
                profiles: [],
            };
        },

        mounted() {
            this.loadProfile();
        },

        methods: {
            loadProfile: async function() {
                try {
                    const response = await profileService.loadProfile();
                    console.log(response);
                    this.profiles = response.data.data;
                    console.log(this.profiles);
                } catch (error) {
                    this.$toast.error("Some error occurred, please refresh!");
                }
            }
        }
    }
</script>

candidate_profile_service.js:

import {http, httpFile} from './http_service';

export function createProfile(data) {
    return httpFile().post('candidate-profile', data);
}

export function loadProfile() {
    return http().get('/candidate-profile/create');
}

下面是控制台中数据的屏幕截图。 enter image description here

2 个答案:

答案 0 :(得分:1)

您的loadProfile方法存在一些问题。您需要访问response.data

loadProfile: async function() {
  try {
    const response = await profileService.loadProfile();
    console.log(response);
    this.profiles = response.data;
    console.log(this.profiles);
  } catch (error) {
    this.$toast.error("Some error occurred, please refresh!");
  }
}

答案 1 :(得分:0)

尝试在已创建与已安装的情况下调用loadProfile:

vec1 <- sapply(df$range, function(x) eval(parse(text = x)))

或添加,然后再将变量分配给this.profiles:

created() {
   this.loadProfile();
}