SpringBoot結(jié)合Vue3實(shí)現(xiàn)簡(jiǎn)單的前后端交互
前端:Vue3
創(chuàng)建項(xiàng)目:
npm create vue@latest > cd <your-project-name> > npm install > npm run dev
項(xiàng)目結(jié)構(gòu)圖如下:
1、查看入口文件內(nèi)容: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}, //定義訪問頁(yè)面的路由地址 ] const router = createRouter({ history:createWebHistory(), routes, }) export default router
4、Axios請(qǐng)求公共方法:utils/axios.js
代碼如下:
import axios from 'axios' //創(chuàng)建axios實(shí)例 const axiosInstance = axios.create({ //api的BaseUrl baseURL : '/aa', setTimeout: 5000, //設(shè)置超時(shí)時(shí)間 responseType: 'json', withDefaults : true, //是否允許帶cookie這些 headers: { 'Content-Type' : 'application/json;charset=utf-8', 'x-token' : '777' } }); export default axiosInstance
5、測(cè)試消息頁(yè)面: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 =>{ ? ? ? ? ? ? //處理響應(yīng)數(shù)據(jù) ? ? ? ? ? ? console.log(response.data); ? ? ? ? ? ? this.msg = response.data; ? ? ? ? }) ? ? ? ? .catch(error =>{ ? ? ? ? ? ? //處理錯(cuò)誤消息 ? ? ? ? ? ? console.error(error); ? ? ? ? }) ? ? ? } ? } </script> <!-- 支持less語(yǔ)法格式 scoped代表樣式只在本組件中起作用 lang="less" --> <style scoped> </style>
6、無代理情況向后端發(fā)請(qǐng)求會(huì)有跨域的問題:
代碼如下:
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': { //目標(biāo)服務(wù)器的地址 target: 'http://localhost:9100/', //是否要將請(qǐng)求中的路徑重寫 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>
測(cè)試代碼:
@RestController @RequestMapping("/login") public class Login { @RequestMapping("/login") public String login(){ return "登錄成功"; } }
到此這篇關(guān)于SpringBoot結(jié)合Vue3實(shí)現(xiàn)簡(jiǎn)單的前后端交互的文章就介紹到這了,更多相關(guān)SpringBoot Vue3前后端交互內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue如何導(dǎo)出文件流獲取附件名稱并下載(在response.headers里解析filename導(dǎo)出)
這篇文章主要介紹了vue如何導(dǎo)出文件流獲取附件名稱并下載(在response.headers里解析filename導(dǎo)出),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07在Vue3項(xiàng)目中使用Jest配置生成測(cè)試報(bào)告的示例詳解
在Vue 3項(xiàng)目中使用Jest進(jìn)行單元測(cè)試是一種常見的做法,它可以幫助我們驗(yàn)證代碼的正確性和穩(wěn)定性,本文將介紹如何在Vue 3項(xiàng)目中配置Jest,以生成測(cè)試報(bào)告,需要的朋友可以參考下2023-09-09Vue運(yùn)用transition實(shí)現(xiàn)過渡動(dòng)畫
vue的過渡動(dòng)畫,主要是transition標(biāo)簽的使用,配合css動(dòng)畫實(shí)現(xiàn)的。接下來通過本文給大家分享Vue運(yùn)用transition實(shí)現(xiàn)過渡動(dòng)畫效果,感興趣的朋友一起看看吧2019-05-05Vue路由傳遞參數(shù)與重定向的使用方法總結(jié)
路由的本質(zhì)就是一種對(duì)應(yīng)關(guān)系,比如說我們?cè)趗rl地址中輸入我們要訪問的url地址之后,瀏覽器要去請(qǐng)求這個(gè)url地址對(duì)應(yīng)的資源,下面這篇文章主要給大家介紹了關(guān)于Vue路由傳遞參數(shù)與重定向的使用方法,需要的朋友可以參考下2022-10-10vue按需加載組件webpack require.ensure的方法
本篇文章主要介紹了vue按需加載組件webpack require.ensure的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12elementUI el-input 只能輸入正整數(shù)驗(yàn)證的操作方法
這篇文章主要介紹了elementUI el-input 只能輸入正整數(shù)驗(yàn)證,本文給大家詳細(xì)講解對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-11-11vue中radio單選框如何實(shí)現(xiàn)取消選中狀態(tài)問題
這篇文章主要介紹了vue中radio單選框如何實(shí)現(xiàn)取消選中狀態(tài)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05