【第8章】Vue之第一个案例程序(前后端交互)

文章目录

  • 前言
  • 一、前端
    • 1. 安装axios
    • 2. 使用axios
    • 3. axios.vue
    • 4. request.js
    • 5. axios.js
  • 二、后端
    • 1.controller
    • 2.entity
    • 三、结果
    • 1. 列表查询
    • 2. 条件查询
  • 总结


前言

接下来我们通过简单的前后端交互来完成界面数据的加载。


一、前端

1. 安装axios

npm install axios

在这里插入图片描述

2. 使用axios

安装完以后,就可以像使用vue组件一样使用axios发起请求了

import axios from 'axios'

3. axios.vue

<!-- 组合式 API -->
<script setup>
import { axiosGetAll,axiosSearch } from '@/api/axios.js'
import { ref, onMounted } from 'vue'

const axiosList = ref([])
const searchConditions=ref({
    category:'',
    state:''
})

async function getAll() {
    axiosList.value = await axiosGetAll();
};
getAll();
async function search() {
	//searchConditions.value也可以用,但是打印出来的数据会看起来很奇怪
    axiosList.value = await axiosSearch({...searchConditions.value});
};
</script>
<template>
    文章分类: <input type="text" v-model="searchConditions.category">

    发布状态: <input type="text" v-model="searchConditions.state">

    <button @click="search">搜索</button>

    <br />
    <br />
    <table border="1 solid" colspa="0" cellspacing="0">
        <tr>
            <th>文章标题</th>
            <th>分类</th>
            <th>发表时间</th>
            <th>状态</th>
            <th>操作</th>
        </tr>
        <tr v-for="axios, index in axiosList">
            <td>{{ axios.title }}</td>
            <td>{{ axios.category }}</td>
            <td>{{ axios.time }}</td>
            <td>{{ axios.state }}</td>
            <td>
                <button>编辑</button>
                <button>删除</button>
            </td>
        </tr>
    </table>
</template>

4. request.js

import axios from 'axios'

const instance = axios.create({
    baseURL: 'http://localhost:8080',
    timeout: 1000,
    headers: { 'X-Custom-Header': 'foobar' }
});
//给自定义的 axios 实例添加拦截器
instance.interceptors.response.use(function (response) {
    // 2xx 范围内的状态码都会触发该函数。
    // 对响应数据做点什么
    return response.data;
  }, function (error) {
    // 超出 2xx 范围的状态码都会触发该函数。
    // 对响应错误做点什么
    console.error(error);
    return Promise.reject(error);
  });
  export default instance;

5. axios.js

import request from '@/utils/request.js'

export function axiosGetAll(){
    return  request.get('/axios/getAll');
}
export function axiosSearch(conditions){
    return request.get('/axios/search',{params:conditions});
}

二、后端

1.controller

package org.example.springboot3.bigevent.controller;

import jakarta.servlet.http.HttpServletResponse;
import org.example.springboot3.bigevent.entity.Axios;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Create by zjg on 2024/6/8
 */
@RestController
@RequestMapping("/axios")
@CrossOrigin//支持跨域
public class AxiosController {
    private List<Axios> articleList = new ArrayList<>();

    {
        articleList.add(new Axios("医疗反腐绝非砍医护收入", "时事", "2023-09-5", "已发布"));
        articleList.add(new Axios("中国男篮缘何一败涂地", "篮球", "2023-09-5", "草稿"));
        articleList.add(new Axios("华山景区已受大风影响阵风达7-8级", "旅游", "2023-09-5", "已发布"));
    }

    //新增文章
    @PostMapping("/add")
    public String add(@RequestBody Axios article) {
        articleList.add(article);
        return "新增成功";
    }

    //获取所有文章信息
    @GetMapping("/getAll")
    public List<Axios> getAll(HttpServletResponse response) {
        return articleList;
    }

    //根据文章分类和发布状态搜索
    @GetMapping("/search")
    public List<Axios> search(String category, String state) {
        return articleList.stream().filter(a -> a.getCategory().equals(category) && a.getState().equals(state)).collect(Collectors.toList());
    }
}

2.entity

package org.example.springboot3.bigevent.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * Create by zjg on 2024/6/8
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Axios {
    private String title;
    private String category;
    private String time;
    private String state;
}

三、结果

1. 列表查询

在这里插入图片描述

2. 条件查询

在这里插入图片描述


总结

回到顶部

前端这些语法呦,用的不太习惯,只能多敲多用,加强练习了。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/710440.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

springboot3 基础特性(1)

文章目录 一、SpringApplication三种方式1.1 基础方式1.2.自定义 SpringApplication1.3、FluentBuilder API 二、自定义Banner三、Profiles3.1 什么是 Profiles &#xff1f;3.2 声明Profiles3.3 激活配置文件3.3.1 分组3.3.2 环境包含3.3.3 激活方式3.3.4 配置优先级 一、Spri…

逻辑这回事(五)---- 资源优化

基础篇 Memory 避免细碎的RAM。将大的RAM拆分成多个小RAM&#xff0c;并根据地址关断可以优化功耗&#xff0c;但把多个小RAM合成大RAM可以优化面积。Block RAM和分布式RAM合理选择。根据存储容量&#xff0c;对Block RAM和分布式RAM的实现面积和功耗进行评估&#xff0c;选择…

Opus从入门到精通(五)OggOpus封装器全解析

Opus从入门到精通(五)OggOpus封装器全解析 为什么要封装 前面Opus从入门到精通(四)Opus解码程序实现提到如果不封装会有两个问题: 无法从文件本身获取音频的元数据(采样率,声道数,码率等)缺少帧分隔标识,无法从连续的文件流中分隔帧(尤其是vbr情况) 针对上面的问题我们可以…

SwiGLU激活函数与GLU门控线性单元原理解析

前言 SwiGLU激活函数在PaLM&#xff0c;LLaMA等大模型中有广泛应用&#xff0c;在大部分测评中相较于Transformer FFN中所使用的ReLU函数都有提升。本篇先介绍LLaMA中SwiGLU的实现形式&#xff0c;再追溯到GLU门控线性单元&#xff0c;以及介绍GLU的变种&#xff0c;Swish激活…

【Windows】DNG Converter(DNG格式转换器)软件介绍

软件介绍 DNG Converter是一款免费软件&#xff0c;用于将数码相机原始RAW图像文件转换为DNG格式&#xff08;数字负片&#xff09;文件。DNG格式是一种数字负片格式&#xff0c;它旨在成为一种行业标准&#xff0c;以便摄影师可以使用一个统一的格式来存储其相机拍摄的原始图…

深度学习笔记: 最详尽估算送达时间系统设计

欢迎收藏Star我的Machine Learning Blog:https://github.com/purepisces/Wenqing-Machine_Learning_Blog。如果收藏star, 有问题可以随时与我交流, 谢谢大家&#xff01; 估算送达时间 1. 问题陈述 构建一个模型来估算在给定订单详情、市场条件和交通状况下的总送达时间。 为…

两个 SASS 分分析案例

1. shfl_sync的 机器 sass 汇编代码 1.1 实验目标 对比 int ret __shfl_sync(0xFFFFFFFF, value, 5, 16); int ret __shfl_sync(0xFFFFFFFF, value, 5, 32); 不同的 sass 汇编代码 1.2 实验代码 源代码 shfl 16&#xff1a; shft_sync_test_16.cu #include <iostream…

LDR6500:手机电脑拓展坞转接器方案的卓越之选

随着科技的飞速发展&#xff0c;手机和电脑已成为我们日常生活中不可或缺的工具。然而&#xff0c;它们的接口有限&#xff0c;经常难以满足我们多样化的需求。这时&#xff0c;一款高效、稳定的拓展坞转接器就显得尤为重要。LDR6500&#xff0c;作为乐得瑞科技精心研发的USB P…

【已解决】引入 element 组件无法使用编译错误 ERROR Failed to compile with 1 error

如果大家使用这个vue 配合 element 框架不熟练&#xff0c;当你顺利按照文档安装好 vue 和 element 的时候想要使用element 的组件时候确无法展示出来&#xff0c;甚至报错。不妨看看是不是这个问题&#xff0c; 1.首先使用element 的时候&#xff0c;前提是把必须要的 elemen…

C++入门 vector介绍及使用

目录 vector的介绍及使用 vector常用接口的介绍及使用 vector的定义 vector iterator 的使用 vector 空间增长问题 vector 增删查改 push_back/pop_back insert & erase & find operator[ ]的遍历 vector的介绍及使用 vector的文档介绍 vector是表示可变大…

热镀锌钢板耐液体性能测 彩钢板抗拉强度检测

钢板检测范围&#xff1a;钢板、彩钢板、不锈钢板、耐磨钢板、合金钢板、压型钢板、冷轧钢板、弹簧钢板、碳钢板、热轧钢板、厚钢板、热镀锌钢板、冲孔钢板、船用钢板、硅钢板、花纹钢板、压力容器钢板、耐候钢板、 钢板检测项目包括化学性能检测、性能检测、机械性能检测、老…

图解Transformer学习笔记

教程是来自https://github.com/datawhalechina/learn-nlp-with-transformers/blob/main/docs/ 图解Transformer Attention为RNN带来了优点&#xff0c;那么有没有一种神经网络结构直接基于Attention构造&#xff0c;而不再依赖RNN、LSTM或者CNN的结构&#xff0c;这就是Trans…

[2024-06]-[大模型]-[Ollama]- WebUI

主要涉及要部署的前端webui是来源于:https://github.com/open-webui/open-webui 正常就使用: docker run -d -p 3000:8080 --add-host=host.docker.internal:host-gateway -v open-webui:/app/backend/data --name open-webui --restart always ghcr.io/open-webui/open-web…

深入浅出 Babel:现代 JavaScript 的编译器

在现代前端开发中&#xff0c;JavaScript 的版本更新速度非常快&#xff0c;新的语法和特性层出不穷。然而&#xff0c;旧版本的浏览器并不总是支持这些新特性。为了确保代码的兼容性和稳定性&#xff0c;我们需要一个工具来将现代 JavaScript 代码转换为旧版本的代码。Babel 就…

vue-element-admin后台集成方案

官网&#xff1a;介绍 | vue-element-adminA magical vue adminhttps://panjiachen.github.io/vue-element-admin-site/zh/guide 1.git环境安装配置及简单操作 1.1git环境安装配置 git软件官网&#xff1a;Git - Downloads (git-scm.com)https://git-scm.com/downloads 下载…

Java | Leetcode Java题解之第145题二叉树的后序遍历

题目&#xff1a; 题解&#xff1a; class Solution {public List<Integer> postorderTraversal(TreeNode root) {List<Integer> res new ArrayList<Integer>();if (root null) {return res;}TreeNode p1 root, p2 null;while (p1 ! null) {p2 p1.left…

JS 中的各种距离 scrollTop?clientHeight?

元素的各种距离 DOM 对象 属性描述offsetWidth只读&#xff0c;返回元素的宽度&#xff08;包括元素宽度、内边距和边框&#xff0c;不包括外边距&#xff09;offsetHeight只读&#xff0c;返回元素的高度&#xff08;包括元素高度、内边距和边框&#xff0c;不包括外边距&am…

【Java】多态、final关键字、抽象类、抽象方法

多态(Polymorphism) 【1】多态跟属性无关&#xff0c;多态指的是方法的多态&#xff0c;而不是属性的多态。 【2】案例代入&#xff1a; public class Animal {//父类&#xff1a;动物&#xff1a; public void shout(){ System.out.println("我是小动物&am…

linux中DNS域名解析服务(后续补充)

分离解析简介&#xff1a; 分离解析的域名服务器实际也是主域名服务器&#xff0c;这里主要是指根据不同的客户端提供不同的域名解析记录。比如来自内网和外网的不同网段地址的客户机请求解析同一域名时&#xff0c;为其提供不同的解析结果。 实验要求&#xff1a;防火墙要么关…

小分子水半幅宽检测 低氘水同位素氘检测 富氢水检测

小分子水半幅宽检测 低氘水同位素氘检测 富氢水检测 检测范围: 矿泉水等饮用水 检测概述 小分子团水活化性很强&#xff0c;具有强渗透力&#xff0c;强溶解力&#xff0c;强扩散力。水的含氧量高&#xff0c;能给人体内的组织细胞带来更多的氧。长自来水大分子团核磁共振测得…