当前位置: 首页 > news >正文

网络请求和状态管理

安装axios

创建项目,输入名称

新建文件夹:

在文件夹下输入cmd

运行

最后在复制到浏览器中

得到页面

使用axios

axios封装成模块

实现一个学生信息案例

模拟数据,请求数据

安装完成后新建一个mock文件夹,制作一个模拟数据

配置模拟数据的信息

App.vue删空

导入

<template>

  <div class="box">

    <button @click="getData">请求数据</button>

    <table>

      <caption>

        <h2>学生信息列表</h2>

      </caption>

      <thead>

        <tr>

          <th>学号</th>

          <th>姓名</th>

          <th>年龄</th>

          <th>性别</th>

          <th>班级</th>

        </tr>

      </thead>

      <tbody>

        <tr v-for="item in studentData.list" :key="item.student_id">

          <td>{{ item.student_id }}</td>

          <td>{{ item.name }}</td>

          <td>{{ item.age }}</td>

          <td>{{ item.gender }}</td>

          <td>{{ item.class }}</td>

        </tr>

      </tbody>

    </table>

  </div>

</template>

<script setup>

import { reactive } from 'vue'

import request from './axios/request.js'

import './mock/mockServer.js'

const studentData = reactive({

  list:[]

})

const getData = function(){

  request({

    url:'/mock/getStudents',

    method:'get'

  }).then(res=>{

    studentData.list = res.data.data

  }).catch(error=>{

    console.log(error);

  })

}

</script>


 

<style>

body,html {

  width: 100%;

  height: 100%;

}

#app {

  width: 100% !important;

}

table {

  border-collapse: collapse;

  margin: 0 auto;

  text-align: center;

}

table td, table th {

  border: 1px solid #cad9ea;

  color: #666;

  height: 30px;

}

table thead th {

  background-color: #cce8eb;

  width: 100px;

}

table tr:nth-child(odd) {

  background: #fff;

}

table tr:nth-child(even) {

  background: #f5fafa;

}

</style>

在main.css和App.vue分别导入

http://www.dtcms.com/a/201038.html

相关文章:

  • ebpf-verifier
  • Flink 非确定有限自动机NFA
  • JavaScript:PC端特效--缓动动画
  • SHELL练习题(1-11题)记录(牛客)
  • Python训练营打卡——DAY30(2025.5.19)
  • python fastapi + react, 写一个图片 app
  • 英特尔硬件笔试面试题型解析
  • YOLO模型使用jupyterlab的方式进行预测/推理(示例)
  • nginx 流量控制
  • ubuntu22.04 卸载ESP-IDF
  • 20250519使用TF卡将NanoPi NEO core开发板刷机为Ubuntu core22.04.3系统完成之后执行poweroff自动关机
  • 实验-使用递归计算阶乘-RISC-V(计算机组成原理)
  • AM32电调学习解读九:ESC上电启动关闭全流程波形分析
  • linux服务器与时间服务器同步时间
  • CSS 选择器入门
  • python文件部署docker,容器路径与系统路径映射
  • DV SSL证书管理主要有哪些功能?
  • java20
  • python动漫论坛管理系统
  • JSX语法之TypeScript-02.1
  • Vue-样式绑定-style
  • Codeforces Round 1025 (Div. 2)(A-D)
  • C++:与7无关的数
  • 绿幕抠图与虚拟背景:直播美颜SDK开发前沿技术探索
  • 基于局部显著位置感知的异常掩码合成方法在CT图像肺部疾病异常检测与病变定位中的应用|文献速递-深度学习医疗AI最新文献
  • 1.portainer
  • AGI大模型(28):LangChain提示模板
  • C++多态的详细讲解
  • CoT(Chain-of-Thought,思维链)推理
  • 【Pandas】pandas DataFrame pct_change