这里是一个简单的样本程序,使用 Vue 3 作为前端框架,Python 的 FastAPI 作为后端 API。首先,我们将创建一个 FastAPI 后端,然后使用 Vue 3 创建前端应用程序来调用后端 API。
后端:FastAPI
- 确保已安装 Python(3.7+)和 pip。
- 安装 FastAPI 和 Uvicorn:
pip install fastapi uvicorn
- 创建一个名为
main.py
的 Python 文件,内容如下:
from fastapi import FastAPI
app = FastAPI()
@app.get("/api/message")
async def get_message():
return {
"message": "Hello from FastAPI!"}
- 使用 Uvicorn 运行后端:
uvicorn main:app --reload --host 0.0.0.0 --port 8000
现在你的 FastAPI 后端已经在 http://localhost:8000 上运行了。
前端:Vue 3
- 确保已安装 Node.js 和 npm。
- 安装 Vue CLI: