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
相关推荐
moxiaoran57531 小时前
uni-app学习笔记五-vue3响应式基础
笔记·学习·uni-app
饕餮争锋1 小时前
org.slf4j.MDC介绍-笔记
java·开发语言·笔记
weixin_448119941 小时前
Datawhale 5月llm-universe 第1次笔记
笔记
chennalC#c.h.JA Ptho2 小时前
lubuntu 系统详解
linux·经验分享·笔记·系统架构·系统安全
m0_689618283 小时前
从海洋生物找灵感:造个机器人RoboPteropod,它能在水下干啥?
笔记·机器人
龙湾开发4 小时前
轻量级高性能推理引擎MNN 学习笔记 02.MNN主要API
人工智能·笔记·学习·机器学习·mnn
HappyAcmen5 小时前
线代第二章矩阵第八节逆矩阵、解矩阵方程
笔记·学习·线性代数·矩阵
愚润求学7 小时前
【递归、搜索与回溯】专题一:递归(二)
c++·笔记·算法·leetcode
愚润求学7 小时前
【Linux】基础 IO(一)
linux·运维·服务器·开发语言·c++·笔记
Wallace Zhang7 小时前
STM32F103_LL库+寄存器学习笔记22 - 基础定时器TIM实现1ms周期回调
笔记·stm32·学习