可以使用axios库来调用接口,并将返回的数据赋值给表格数据data

首先,需要安装axios库:

npm install axios

然后,在组件的setup函数中引入axios库,并定义data变量:

import { reactive } from 'vue';
import axios from 'axios';

export default {
  setup() {
    const data = reactive({
      tableData: []
    });

    // 调用接口并赋值给表格数据
    const getIndexApi = async () => {
      try {
        const response = await axios.get('your-api-url');
        data.tableData = response.data;
      } catch (error) {
        console.error(error);
      }
    };

    getIndexApi(); // 在组件加载时调用接口

    return {
      data
    };
  }
}

在上述代码中,通过reactive函数将data对象变成响应式的,这样当tableData属性的值发生变化时,组件会自动重新渲染。

getIndexApi函数中,使用axios.get方法发送GET请求到指定的接口URL,并将返回的数据赋值给data.tableData属性。

最后,在组件的模板中可以通过data.tableData来访问表格数据。

<template>
  <table>
    <tr v-for="item in data.tableData" :key="item.id">
      <td>{{ item.name }}</td>
      <td>{{ item.age }}</td>
      ...
    </tr>
  </table>
</template>
``
vue3 、给定接口getIndexApi 调用接口 给表格数据 data赋值

原文地址: http://www.cveoy.top/t/topic/hOl4 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录