欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot結(jié)合Vue3實(shí)現(xiàn)簡單的前后端交互

 更新時間:2023年08月24日 11:48:44   作者:zhang2383906154  
本文主要介紹了SpringBoot結(jié)合Vue3實(shí)現(xiàn)簡單的前后端交互,結(jié)合實(shí)際案例,說明了如何實(shí)現(xiàn)前后端數(shù)據(jù)的交互,具有一定的?參考價(jià)值,感興趣的可以了解一下

前端: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}, //定義訪問頁面的路由地址
]
const router = createRouter({
    history:createWebHistory(),
    routes,
})
export default router

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

代碼如下:

import axios from 'axios'
//創(chuàng)建axios實(shí)例
const axiosInstance = axios.create({
    //api的BaseUrl 
    baseURL : '/aa',
    setTimeout: 5000, //設(shè)置超時時間
    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 =>{
? ? ? ? ? ? //處理響應(yīng)數(shù)據(jù)
? ? ? ? ? ? console.log(response.data);
? ? ? ? ? ? this.msg = response.data;
? ? ? ? })
? ? ? ? .catch(error =>{
? ? ? ? ? ? //處理錯誤消息
? ? ? ? ? ? console.error(error);
? ? ? ? })
? ? ? }
? }
</script>
<!-- 支持less語法格式 scoped代表樣式只在本組件中起作用 lang="less" -->
<style scoped>
</style>

6、無代理情況向后端發(fā)請求會有跨域的問題:

代碼如下:

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/',
            //是否要將請求中的路徑重寫
            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 "登錄成功";
    }
}

 到此這篇關(guān)于SpringBoot結(jié)合Vue3實(shí)現(xiàn)簡單的前后端交互的文章就介紹到這了,更多相關(guān)SpringBoot Vue3前后端交互內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 使用vuex較為優(yōu)雅的實(shí)現(xiàn)一個購物車功能的示例代碼

    使用vuex較為優(yōu)雅的實(shí)現(xiàn)一個購物車功能的示例代碼

    這篇文章主要介紹了使用vuex較為優(yōu)雅的實(shí)現(xiàn)一個購物車功能的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • Vue中android4.4不兼容問題的解決方法

    Vue中android4.4不兼容問題的解決方法

    這篇文章主要介紹了Vue中android4.4不兼容問題的解決方法,需要的朋友可以參考下
    2018-09-09
  • Element-ui?DatePicker日期選擇器基礎(chǔ)用法示例

    Element-ui?DatePicker日期選擇器基礎(chǔ)用法示例

    這篇文章主要為大家介紹了Element-ui?DatePicker日期選擇器基礎(chǔ)用法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • Vue + Vue-router 同名路由切換數(shù)據(jù)不更新的方法

    Vue + Vue-router 同名路由切換數(shù)據(jù)不更新的方法

    本篇文章主要介紹了Vue + Vue-router 同名路由切換數(shù)據(jù)不更新的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • vue3使用flv.js播放flv直播流的代碼示例

    vue3使用flv.js播放flv直播流的代碼示例

    這篇文章主要介紹了vue3使用flv.js播放flv直播流的相關(guān)資料,詳細(xì)描述了安裝flv.js和編寫HTML、JavaScript代碼的過程,包括在頁面關(guān)閉時銷毀播放器的操作,需要的朋友可以參考下
    2025-05-05
  • Vue3+Vite項(xiàng)目部署后刷新白屏問題的解決方法

    Vue3+Vite項(xiàng)目部署后刷新白屏問題的解決方法

    Vue3 + Vite 項(xiàng)目部署后出現(xiàn)了手動刷新頁面時出現(xiàn)白屏的問題,下面小編就來和大家探討一下白屏出現(xiàn)的原因以及具體的解決方法,需要的可以參考下
    2025-03-03
  • Vue3中使用混入(Mixin)的示例詳解

    Vue3中使用混入(Mixin)的示例詳解

    混入(Mixin)是?Vue?中一種代碼復(fù)用的模式,允許將組件的選項(xiàng)抽離為獨(dú)立模塊,下面就跟隨小編一起來深入了解下如何在Vue3中使用混入Mixin吧
    2025-03-03
  • vue3的組件通信&v-model使用實(shí)例詳解

    vue3的組件通信&v-model使用實(shí)例詳解

    props 主要用于父組件向子組件通信,再父組件中通過使用:msg='msg'綁定需要傳給子組件的屬性值,然后再在子組件中用props接收該屬性值,這篇文章主要介紹了vue3的組件通信&v-model使用,需要的朋友可以參考下
    2024-05-05
  • vue-pdf如何通過文件流預(yù)覽pdf文件

    vue-pdf如何通過文件流預(yù)覽pdf文件

    這篇文章主要介紹了vue-pdf如何通過文件流預(yù)覽pdf文件問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • vue中keep-alive組件的用法示例

    vue中keep-alive組件的用法示例

    眾所周知keep-alive是Vue提供的一個抽象組件,主要是用來對組件進(jìn)行緩存,從而做到節(jié)省性能,這篇文章主要給大家介紹了關(guān)于vue中keep-alive組件用法的相關(guān)資料,需要的朋友可以參考下
    2021-05-05

最新評論