vite5+vue3开发阅读APP实战笔记20240725

目前界面长成这样:

配置别名

修改vite.config.js

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

// https://vitejs.dev/config/
export default defineConfig({
    server: {
        open: true,
        port: 8088,
    },
    plugins: [vue()],
    resolve: {
        alias: {
            "@": path.resolve(__dirname, "src"),
        }
    }
})

这里配置别名的核心代码是:

js 复制代码
"@": path.resolve(__dirname, "src"),

配置首页

创建 src/page/home/index.vue

html 复制代码
<script setup>

</script>

<template>
home
</template>

<style scoped>

</style>

修改src/router/index.js,这里主要是配置首页的路由:

js 复制代码
import { createWebHashHistory, createRouter } from 'vue-router'

const routes = [
    {
        path: "/",
        name: "Home",
        component: () => import("@/page/home/index.vue")
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes,
})

export default router

修改 src/main.js

js 复制代码
import { createApp } from 'vue'
import router from './router'

import App from './App.vue'

const app = createApp(App)

app.use(router)

app.mount('#app')

修改src/App.vue

html 复制代码
<script setup>
</script>

<template>
  <RouterView/>
</template>

此时界面的预览效果如下:

引入外部资源

将提前准备好的资源放入assets目录:

修改src/main.js,引入静态资源:

js 复制代码
import { createApp } from 'vue'
import router from './router'
import App from './App.vue'

import "@/assets/css/common.css"
import "@/assets/font/iconfont.css"

const app = createApp(App)

app.use(router)

app.mount('#app')

引入vant

参考文档:https://vant-ui.github.io/vant/#/zh-CN/quickstart

安装:

bash 复制代码
pnpm i vant

安装按需引入依赖:

bash 复制代码
pnpm add @vant/auto-import-resolver unplugin-vue-components unplugin-auto-import -D

修改 vite.config.js

js 复制代码
import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import path from "path"
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import {VantResolver} from '@vant/auto-import-resolver';

// https://vitejs.dev/config/
export default defineConfig({
    server: {
        open: true,
        port: 8088,
    },
    plugins: [
        vue(),
        AutoImport({
            resolvers: [VantResolver()],
        }),
        Components({
            resolvers: [VantResolver()],
        }),
    ],
    resolve: {
        alias: {
            "@": path.resolve(__dirname, "src"),
        }
    }
})

修改src/page/home/index.vue,使用组件和API。注意,这里无论是组件还是API,都不需要我们手动引入,我们配置了自动导入以后,vite会帮我们自动导入。

html 复制代码
<script setup>
showToast('No need to import showToast');
</script>

<template>
  home
  <van-button type="primary"/>
</template>

<style scoped>

</style>

此时预览效果如下:

划分页面结构

四个页面,对应四个底部导航:

  • me:我的
  • home:首页
  • welfare:福利
  • community:社区

然后添加layout用于存放布局组件,添加底部导航组件:

修改 src/router/index.js,配置路由:

js 复制代码
import { createWebHashHistory, createRouter } from 'vue-router'

const routes = [
    {
        path: "/",
        component: () => import("@/page/home/index.vue")
    },
    {
        path: "/community",
        component: () => import("@/page/community/index.vue")
    },
    {
        path: "/welfare",
        component: () => import("@/page/welfare/index.vue")
    },
    {
        path: "/me",
        component: () => import("@/page/me/index.vue")
    },
]

const router = createRouter({
    history: createWebHashHistory(),
    routes,
})

export default router
相关推荐
m0_651562522 分钟前
2026/3/26 学习笔记——终端复用工具screen
笔记·学习
sinat_255487819 分钟前
JSON·学习笔记
java·开发语言·笔记·算法
Roselind_Yi26 分钟前
从线性回归实战到Python依赖安装踩坑:我的机器学习入门排雷记
笔记·python·算法·机器学习·回归·线性回归·学习方法
宵时待雨42 分钟前
C++笔记归纳15:红黑树
开发语言·数据结构·c++·笔记
轻赚时代1 小时前
零开发门槛!AI视频工具实操教程:图片/文字一键生成动态视频
人工智能·经验分享·笔记·音视频·创业创新·课程设计
看山是山_Lau1 小时前
如何封装和定义一个函数
c语言·开发语言·c++·笔记
adamlevine71 小时前
【docker笔记-001】如何设置docker使得容器能在多个numa之间均匀使用内存
笔记·docker·容器·k8s·numa·numactl·k3s
1104.北光c°1 小时前
Leetcode3.无重复字符的最长子串 HashSet+HashMap 【hot100算法个人笔记】【java写法】
java·开发语言·笔记·程序人生·算法·leetcode·滑动窗口
鹭天1 小时前
目标检测学习笔记
笔记·学习·目标检测
-许平安-1 小时前
MCP项目笔记五(PluginAPI)
c++·笔记·rpc·json·mcp·pluginapi