async get和post请求接口数据 (增删改查分页)

1.async get请求接口数据

async xzlist() {
  const { data: res } = await this.$http_message.post(
    "/shift_change_log/list",
    this.queryList
  );
  if (res.code == 0) {
    console.log(res.data);
    this.list = res.data.rows;
    this.total = res.data.total;
  } else {
    return this.$message.error("获取用户信息失败!");
  }
},

queryList看到这里大家可能是看懵了吧
这里面我们放的是我们请求的参数 ID name 或者分页参数
例如:

  queryList: {
        asc: true,
        orderBy: "id",
        pageNum: 1,
        pageSize: 10,
         regDateBeginTime: "2001-01-01",
         regDateEndTime: "2001-01-01",
         shiftId: 0,
         status: 1,
        teamId: 10,
      },

xzlist这个为事件名 我们可以在mounted 或者created 中进行挂载。

 created() {
    // 进入页面调用部门用户信息
    this.xzlist();
  },

我们可以在console.log()打印一下我们在控制台看下
在这里插入图片描述
同时更方便的是我们在后台network中看接口请求信息更为方便。
在这里插入图片描述
这样我们数据就通过list数组渲染到页面上来了
在这里插入图片描述
添加的话我们用post请求
同上把请求的参数放到addForm

//新增部门
    async addDepartment() {
      const { data: res } = await this.$http_message.post(
        "/fault_info/create",
        this.addForm
      );
      console.log(res.data);
      if (res.code == 0) {
        this.$message.success(res.msg);
        this.ediDialogVisible = false;
        this.xzlist();
      } else {
        return this.$message.error("添加部门失败!");
      }
    },

编辑的话我们首先点击编辑按钮要取到当前id的数据 回显

  //修改部门
    async showEditDialog(id) {
      this.addDialogVisible = true;
      const { data: res } = await this.$http_message.get(
        "fault_info/getDataById?id=" + id
      );
      if (res.code == 0) {
        console.log(res.data);
        this.ediForm = res.data;
      } else {
        console.log("失败");
      }
    },

其次是编辑修改页面弹框

async eidtUserInfo() {
  const { data: res } = await this.$http_base.post(
    "parameters/main/update",
    this.ediForm
  );
  if (res.code == 0) {
    this.$message.success("修改部门成功");
    this.addDialogVisible = false;
    this.xzlist();
  } else {
    console.log("失败");
    return this.$message.error("修改部门失败!");
  }
},

可能有人对**$http_base**这个比较陌生了 这个是我们的服务器请求
因为我们好几个服务器 为了区分在这里插入图片描述

配置一个请求后端服务的api
在这里插入图片描述
删除大家就不陌生了 可能我最喜欢的就是删除了 直接通过id删除

  //删除用户
    async removeUserById(id) {
      const confirmResult = await this.$confirm(
        "你确定要删除当前用户吗",
        "删除",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => {
        return err;
      });
      if (confirmResult !== "confirm") {
        return this.$message.info("取消删除成功");
      } else {
        const { data: res } = await this.$http_message.get(
          "fault_info/delete?id=" + id,
          this.delete
        );
        if (res.code == 0) {
          this.$message.success("删除用户成功");
          this.xzlist();
        } else {
          return this.$message.error("删除用户失败!");
        }
      }
    },

分页也是必不可少的一部分(html部门)

  <p style="margin-top: 20px; color: #fff">共{{ total }}条记录</p>
      <div class="fenye">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryList.page"
          :page-sizes="[5, 10, 15]"
          :page-size="queryList.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>

methods:里面写事件

handleSizeChange(newSize) {
  console.log(newSize);
  this.queryList.pageSize = newSize;
  this.xzlist();
},
handleCurrentChange(newpage) {
  console.log(newpage);
  this.queryList.pageNum = newpage;
  this.xzlist();
},

美中不足 还请多多提议

ASP.NET Core Web API是一个轻量级的应用程序框架,用于构建RESTful Web服务。要实现增删改查(CRUD)操作,你需要创建控制器、模型以及数据库相关的实体。 **步骤如下:** 1. **模型(Model):** - 定义数据模型(通常是Poco类),比如`Product.cs`,包含属性如`Id`, `Name`, `Price`等。 ```csharp public class Product { public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } } ``` 2. **数据库设置:** 如果使用EF Core,添加上下文并配置数据库连接。 ```csharp public class ApplicationDbContext : DbContext { // ... 省略DbSet<Product> Products 等映射关系 protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer("连接字符串"); } // ... 省略其他生命周期方法 } ``` 3. **创建API控制器(Controller):** - 创建如`ProductsController.cs`,用于处理CRUD请求。例如,`GetAll()`获取所有产品,`AddProduct(product)`接收POST请求添加新产品等。 ```csharp [Route("api/[controller]")] [ApiController] public class ProductsController : ControllerBase { private readonly ApplicationDbContext _context; public ProductsController(ApplicationDbContext context) { _context = context; } // GET api/products [HttpGet] public async Task<ActionResult<IEnumerable<Product>>> GetAll() { return await _context.Products.ToListAsync(); } // POST api/products [HttpPost] public async Task<ActionResult<Product>> AddProduct(Product product) { _context.Products.Add(product); await _context.SaveChangesAsync(); return CreatedAtAction(nameof(GetAll), new { id = product.Id }, product); } // 其他 CRUD 方法,如 [HttpGet("{id}")] // [HttpPut("{id}")] // [HttpDelete("{id}")] } ``` **相关问题:** 1. ASP.NET Core Web API如何支持分页查询? 2. 如何实现更复杂的CRUD操作,如搜索过滤功能? 3. 如何验证用户输入并处理错误状态码?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值