vue3前端开发的基础教程——快速上手

【前言】这里使用的技术栈:fastapi+vue3+pycharm

一、创建vue3项目

在项目的文件夹使用下面命令创建vue3前端框架代码

bash 复制代码
npm create vite@latest frontend

选择框中选择:

  • Framework: Vue
  • Variant: JavaScript 或 TypeScript
bash 复制代码
cd frontend
npm install

启动本地开发

bash 复制代码
npm run dev

一、vue3项目编写代码

frontend>src>App.vue中编写网站首页的代码,如下面

javascript 复制代码
<script setup>
const name = '世界'
</script>

<template>
  <h1>你好,{{ name }}!</h1>
</template>

<style scoped>
.logo {
  height: 6em;
  padding: 1.5em;
  will-change: filter;
  transition: filter 300ms;
}
.logo:hover {
  filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.vue:hover {
  filter: drop-shadow(0 0 2em #42b883aa);
}
</style>

npm run dev启动项目就可以在本地愉快地编写代码啦!

三、打包发布

代码在本地编写调试完成后就可以打包发布了
frontend\vite.config.js文件修改静态资源路由

javascript 复制代码
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

// https://vite.dev/config/
export default defineConfig({
  base: './',   // 增加这个代码
  plugins: [vue()],
})

然后运行命令打包静态资源文件,自动生成dist文件夹

bash 复制代码
cd frontend
npm run build

四、fastapi部署上线

python 复制代码
from fastapi import FastAPI, Request
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse, PlainTextResponse
import uvicorn

app = FastAPI()

# 静态文件托管(JS, CSS, 图片等)
app.mount("/", StaticFiles(directory="../frontend/dist", html=True), name="static")


# 首页路由,返回 index.html
@app.get("/")
async def read_index():
    return FileResponse("../frontend/dist/index.html")


if __name__ == '__main__':
    uvicorn.run("main:app", host="127.0.0.1", port=8080)
相关推荐
kyriewen4 小时前
Git Commit 前自动修复代码风格?配置 Husky + lint-staged,从此 CR 只聊逻辑
前端·git·面试
岁月宁静4 小时前
RAG 文档摄入全链路,从原理到生产落地
vue.js·人工智能·python
小和尚同志5 小时前
AI 自动化测试探索(一):Playwright MCP
前端·人工智能·aigc
老马识途2.05 小时前
在AI的帮助下理解spring的启动过程
java·前端·spring
徐小夕5 小时前
Loop Engineering 深度解析与实战指南(全网最全)
前端·算法·github
运筹vivo@6 小时前
Python ContextVar 底层机制与内存模型拆解
前端·数据库·python
#麻辣小龙虾#7 小时前
基于vue3.0开发一款【固废与废气运维管理系统】(支持源码)
前端·vue.js·vue3
Cosolar7 小时前
Docsify零构建文档站完全指南:从快速搭建到企业级部署
前端·开源·github
weixin_471383037 小时前
Taro-02-页面路由
前端·taro
星栈独行7 小时前
Makepad 应用如何读文件、调接口、保存数据
前端·程序人生·ui·rust·github