后端传来json数据,vue前端如何渲染??

例如后端传来:

在这里插入图片描述

发现里面有嵌套格式的数据,我们该如何渲染呢??下面上代码:


<template>
  <el-table
      :data="tableData"
      stripe
      style="width: 100%">
    <el-table-column
        label="ID"
        prop="id">
    </el-table-column>
    <el-table-column
        label="Name"
        prop="username">
    </el-table-column>
    <el-table-column
        label="权限"
        prop="roles[0].nameZh">
    </el-table-column>
    <el-table-column
        align="right">
    </el-table-column>
  </el-table>
</template>


<script>
export default {
  data() {
    return {
      tableData: [],
    }
  },
  methods: {
    findAll(){
      this.axios.get('http://localhost:8989/users').then(res=>{
        this.tableData=res.data._embedded.users;
        console.log(this.tableData);
      })
    },
  },
  created() {
    this.findAll();
  }
}
</script>

<style>

</style>

  • 5
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
假设后端传来数据格式如下: ```json { "weekdays": [ { "day": "Monday", "courses": [ { "name": "Math", "time": "9:00-10:30", "location": "Room 101" }, { "name": "Physics", "time": "11:00-12:30", "location": "Room 102" } ] }, { "day": "Tuesday", "courses": [ { "name": "English", "time": "9:00-10:30", "location": "Room 103" }, { "name": "History", "time": "11:00-12:30", "location": "Room 104" } ] }, // ... ] } ``` 我们可以通过 Vue 的模板语法将数据渲染到页面上,示例代码如下: ```html <template> <div> <table> <thead> <tr> <th>Weekday</th> <th>Course</th> <th>Time</th> <th>Location</th> </tr> </thead> <tbody> <tr v-for="weekday in weekdays" :key="weekday.day"> <td>{{ weekday.day }}</td> <td v-for="course in weekday.courses" :key="course.name">{{ course.name }}</td> <td v-for="course in weekday.courses" :key="course.time">{{ course.time }}</td> <td v-for="course in weekday.courses" :key="course.location">{{ course.location }}</td> </tr> </tbody> </table> </div> </template> <script> export default { data() { return { weekdays: [] } }, mounted() { // 假设后端接口为 /api/schedule axios.get('/api/schedule').then(response => { this.weekdays = response.data.weekdays }) } } </script> ``` 这段代码中,我们使用了 Vue 的模板语法和 v-for 指令进行数据渲染。由于课表是以表格的形式呈现,我们使用了 table 元素和相应的表头和表体。在表体中,我们使用了 v-for 指令来遍历数据,并将数据展示到表格中。 在 mounted 钩子函数中,我们向后端接口发送请求,并将返回的数据赋值给 weekdays 数组,从而完成了课表的展示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

结构化思维wz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值