FastApi+Vue+LayUI實現(xiàn)前后端分離的示例代碼
前言
在前面的Api開發(fā)中,我們使用FastApi已經(jīng)可以很好的實現(xiàn)。但是實際使用中,我們通常建議前后端項目分離。今天我們就使用FastApi+Vue+LayUI做一個前后端分離的Demo。
項目設(shè)計
后端
后端我們采用FastApi在新的test視圖中,定義一個路由,并將其注冊到app中,并且在test視圖中定義一個接口,實現(xiàn)模擬從數(shù)據(jù)庫讀取數(shù)據(jù)供前端調(diào)用渲染。
代碼
test.py
from fastapi import FastAPI,Depends,Header,HTTPException,APIRouter
from fastapi.param_functions import Body
from starlette.requests import Request
from starlette.templating import Jinja2Templates
from starlette import status
import uvicorn
from deta import Deta
from fastapi.responses import StreamingResponse
from fastapi.responses import JSONResponse
# 實例化路由器
router = APIRouter()
templates = Jinja2Templates('templates')
# 注意,視圖這里使用router來聲明請求方式&URI
@router.get('/info')
def user_list():
# vue的響應(yīng)數(shù)據(jù)
items = [
{'id':'1','name':'phyger'},
{'id':'2','name':'fly'},
{'id':'3','name':'enheng'},
]
return JSONResponse(content=items)
@router.get('/')
def welcome():
return "這里是測試路由"
'''
實際上,這里的home.html也是需要前端服務(wù)去向用戶渲染的,
但是我們?yōu)榱朔奖阊菔荆磫忧岸朔?wù)器,直接將前端代碼寫在了home.html中,
實際上,當(dāng)用戶請求/check的時候,前端代碼會去請求/info接口獲取數(shù)據(jù),
從而實現(xiàn)前端頁面的數(shù)據(jù)渲染。
'''
@router.get('/check')
def home(request:Request):
return templates.TemplateResponse(name='home.html',context={'request':request,})
前端
前端我們直接導(dǎo)入Vue、LayUI、Axios的JS和CSS的CDN資源,在Vue實例的mount階段,使用axios調(diào)用后端接口拿到數(shù)據(jù),使用LayUI的樣式對table元素進行美化。
代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://unpkg.com/vue@next"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<!-- 引入 layui.css -->
<link rel="stylesheet" rel="external nofollow" />
<!-- 引入 layui.js -->
<script src="https://www.layuicdn.com/layui/layui.js" type="text/javascript" charset="utf-8"></script>
<title>Home</title>
</head>
<body>
<div id="app">
<table class="layui-table">
<tr v-for="p in infos">
<td>[[ p.id ]]</td>
<td>[[ p.name ]]</td>
</tr>
</table>
</div>
<table id="test" class="layui-table"></table>
<script type="text/javascript">
const Vapp = Vue.createApp({
data() {
return {
infos: [{id:1,name:'phyger'}],
info: "hello vue..."
}
},
mounted() {
this.showinfo();
},
methods: {
showinfo(){
axios.get('/test/info')
.then(response=>{
this.infos=response.data;
console.log(response);
console.log(this.infos);
})
,err=>{
console.log(err);
};
},
},
})
Vapp.config.compilerOptions.delimiters = ['[[', ']]']
Vapp.mount('#app')
</script>
</body>
</html>
運行項目
啟動 FastApi 后端服務(wù)器,訪問 /test/check 接口。

Q&A
Q:為什么在請求/info 接口總會出現(xiàn)一個 Temporary Redirect 重定向呢?

A:原因是因為我們在 FastApi 接口定義的時候,uri 的格式不規(guī)范導(dǎo)致,uri 的結(jié)尾不需要/,如果你接口增加了/,我們使用瀏覽器訪問 uri,瀏覽器會忽略結(jié)尾的/,F(xiàn)astApi 會在內(nèi)部進行查重定向,將瀏覽器不帶/的請求重定向到我們定義的帶/的視圖函數(shù)上。
到此這篇關(guān)于FastApi+Vue+LayUI實現(xiàn)前后端分離的示例代碼的文章就介紹到這了,更多相關(guān)FastApi+Vue+LayUI 前后端分離內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
uniapp使用v-loading并且不引入element-ui的操作方法
這篇文章主要介紹了uniapp使用v-loading并且不引入element-ui,首先創(chuàng)建loading.js,創(chuàng)建lloading.scss,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-10-10
基于Vue2的獨立構(gòu)建與運行時構(gòu)建的差別(詳解)
下面小編就為大家分享一篇基于Vue2的獨立構(gòu)建與運行時構(gòu)建的差別詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12
詳解VUE自定義組件中用.sync修飾符與v-model的區(qū)別
這篇文章主要介紹了詳解VUE自定義組件中用.sync修飾符與v-model的區(qū)別,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06

