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

SpringBoot +Vue3 简单的前后端交互

前端:Vue3

创建项目:

npm create vue@latest

> cd <your-project-name>

> npm install

> npm run dev

项目结构图如下:

1、查看入口文件内容:main.js

代码如下:

import './assets/main.css'
import { createApp } from 'vue'
import App from './App.vue'
import router  from '@/router'
//import axios from 'axios'// console.log(App)
const app = createApp(App)
//app.config.globalProperties.$axios = axios
app.use(router)
// app.use(axios)app.mount('#app')

在main.js中,首先引入了Vue组件和APP根组件

2、APP跟组件代码如下:

<template><div id="app"><!-- App跟组件 --><router-view></router-view></div></template><script setup>name: 'app'
</script><style scoped></style>

3、路由文件配置:router/index.js

代码如下:

import { createRouter,createWebHistory } from 'vue-router'
import Login from '../components/Login.vue' //引用Login组件const routes = [{path: '/',redirect: '/login'},{path: '/login',component: Login}, //定义访问页面的路由地址]const router = createRouter({history:createWebHistory(),routes,
})export default router

4、Axios请求公共方法:utils/axios.js

代码如下:

import axios from 'axios'
//创建axios实例
const axiosInstance = axios.create({//api的BaseUrl baseURL : '/aa',setTimeout: 5000, //设置超时时间responseType: 'json',withDefaults : true, //是否允许带cookie这些headers: {'Content-Type' : 'application/json;charset=utf-8','x-token' : '777'}
});export default axiosInstance

5、测试消息页面:components/Login.vue

代码如下:

<template><header><img alt="Vue logo" class="logo" src="../assets/logo.svg" width="125" height="125" /><div class="wrapper">登录组件:{{ msg }}<button onclick="login"> axios</button></div></header></template><script>
import axiosInstance from '../utils/Axios'export default {data(){return {msg : '开始'}},mounted(){axiosInstance.get('login/login').then(response =>{//处理响应数据console.log(response.data);this.msg = response.data;}).catch(error =>{//处理错误消息console.error(error);})}}
</script><!-- 支持less语法格式 scoped代表样式只在本组件中起作用 lang="less" -->
<style scoped></style>

6、无代理情况向后端发请求会有跨域的问题:

代码如下:

import { fileURLToPath, URL } from 'node:url'import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'node:path'// https://vitejs.dev/config/
export default defineConfig({plugins: [vue(),],resolve: {alias: {'@': fileURLToPath(new URL('./src', import.meta.url))}},server: {proxy: {//需要代理的路径'/aa': {//目标服务器的地址target: 'http://localhost:9100/',//是否要将请求中的路径重写rewrite: path => path.replace(/^\/api/,''),//是否要改变代理的源地址changeOrigin: true,//其他可选的代理配置}}}
})

后端代码:

引入的jar包:

  <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>

测试代码:


@RestController
@RequestMapping("/login")
public class Login {@RequestMapping("/login")public String login(){return "登录成功";}
}

http://www.lryc.cn/news/137553.html

相关文章:

  • 【Android】Mobile-Security-Framework-MobSF Manifest 静态扫描规则
  • 【C++】初谈迭代器
  • PL端案例开发手册
  • 华为OD-整数对最小和
  • Ubuntu 22LTS 配置静态IP
  • 【Python】Python爬虫:网络数据的提取利器
  • 20.图的遍历
  • ARM DIY(一)电源、SD卡座、SOC 调试
  • 数学建模知识之小白入门篇
  • 【日常积累】Linux下ftp服务安装
  • 确定了,TikTok将于9月12日正式关闭美国半闭环
  • ATFX汇评:英国7月零售销售年率大降,GBPUSD仍未升破1.3000
  • CTFhub-sqli注入-Referer注入
  • 【案例】登录注册
  • Unity 物体的运动之跟随鼠标
  • C++基础Ⅱ变量
  • Linux管理SpringBoot应用shell脚本实现
  • 一篇搞懂浏览器的工作原理(万字详解)
  • C语言调用python训练的机器学习模型(项目需求轻体量)
  • get和post请求的区别以及post请求的url参数问题
  • android NullPointerException externalCacheDir
  • 设计模式-过滤器模式(使用案例)
  • 成功解决修改已经push到远程git仓库的commit message
  • Ubuntu18.04 交叉编译openssl-1.1.1
  • 七夕学算法
  • 在C++中利用rapidjson实现Python中的字典(Dict)
  • 数组和指针练习(3)
  • 如何用树莓派Pico针对IoT编程?
  • 【填坑向】MySQL常见报错及处理系列(ERROR! The server quit without updating PID file)
  • 如何处理MySQL自增ID用完