Vue vite 框架

锋选菁英招聘管理平台项目

一、项目架构搭建

1. vite搭建项目

Vite下一代的前端工具链,为开发提供极速响应。

  1. 创建项目

yarn create vite
填项目名
选vue
选Typescript
  1. 安装依赖

cd fxjy-admin
yarn
  1. 启动项目

yarn dev

2.目录架构认识及改造

src
    api    管理异步请求方法
    components  公共组件
    layout    基本布局骨架
    store   状态机
    router   路由
    utils   公共方法包
    views   业务组件(页面)
    assets   静态资源(img、css)
    App.vue   根组件
    main.ts   入口文件

3. vite配置路径别名@

  1. vite.config.ts配置

     ...
     import { join } from "path";
     export default defineConfig({
       ...
       resolve: {
         alias: {
           "@": join(__dirname, "src"),
         },
       },
     });
    ​
  2. 根目录新建 tsconfig.json 配置

    {
       "compilerOptions": {
         "experimentalDecorators": true,
         "baseUrl": "./",
         "paths": {
           "@/*": ["src/*"],
           "components/*": ["src/components/*"],
           "assets/*": ["src/assets/*"],
           "views/*": ["src/views/*"],
           "common/*": ["src/common/*"]
         }
       },
       "exclude": ["node_modules", "dist"]
     }
    ​
  3. 安装path的类型提示

    yarn add @types/node

  4. 重启 vscode

4. AntDesign 组件库

Ant Design Vue 文档

  1. 安装组件库

yarn add ant-design-vue
  1. 按需导入辅助包

yarn add unplugin-vue-components --dev
  1. 修改vite.config.ts配置

import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import Components from "unplugin-vue-components/vite";
import { AntDesignVueResolver } from "unplugin-vue-components/resolvers";
// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    Components({
      resolvers: [AntDesignVueResolver()],
    }),
  ],
  ...
});
​
  1. 在任意.vue组件中,直接使用组件,如App.vue

​
<script setup lang="ts">
</script>
​
<template>
  <a-button type="primary">Primary Button</a-button>
</template>
​
<style scoped>
</style>
​

5. 搭建主面板骨架

  1. 从antd-vue官方获取layout布局

不要照着代码敲,直接复制即可,然后只需要为a-layout容器添加高度

// src/layout/index.vue 主要的布局文件
<template>
  <a-layout style="min-height: 100vh">
    <SiderMenu />
    <a-layout>
      <a-layout-header style="background: #fff; padding: 0" />
      <a-layout-content style="margin: 0 16px">
        <a-breadcrumb style="margin: 16px 0">
          <a-breadcrumb-item>User</a-breadcrumb-item>
          <a-breadcrumb-item>Bill</a-breadcrumb-item>
        </a-breadcrumb>
        <div
          :style="{ padding: '24px', background: '#fff', minHeight: '360px' }"
        >
          Bill is a cat.
        </div>
      </a-layout-content>
      <a-layout-footer style="text-align: center">
        Ant Design ©2018 Created by Ant UED
      </a-layout-footer>
    </a-layout>
  </a-layout>
</template>
<script setup lang="ts">
import {
  PieChartOutlined,
  DesktopOutlined,
  UserOutlined,
  TeamOutlined,
  FileOutlined,
} from "@ant-design/icons-vue";
import { ref } from "vue";
import SiderMenu from "./components/sider-menu.vue";
const collapsed = ref(false);
const selectedKeys = ref(["1"]);
</script>
<style scoped>
.logo {
  height: 32px;
  margin: 16px;
  background: rgba(255, 255, 255, 0.3);
}
​
.site-layout .site-layout-background {
  background: #fff;
}
[data-theme="dark"] .site-layout .site-layout-background {
  background: #141414;
}
</style>
  1. App.vue引入 主界面的布局文件

<script setup lang="ts">
import MainLayout from "@/layout/index.vue";
</script>
​
<template>
  <MainLayout />
</template>
​
<style scoped></style>
  1. 查看浏览器,预览运行结果

二、VueRouter路由运用

2.1.路由基本配置

  1. 安装

    默认安装的是vue-router@4,使用时需要注意版本号

    yarn add vue-router
  2. 新建页面组件

    └─views
        │  dashboard.vue   可视化图表页
        ├─category     分类管理
        │      list.vue
        │      pub.vue
        └─job          岗位管理
                list.vue
                pub.vue
  3. 配置路由

    // src/router/index.ts
    import { createRouter, createWebHashHistory } from "vue-router";
    ​
    const router = createRouter({
      history: createWebHashHistory(),
      routes: [
        {
          path: "/",
          component: () => import("@/views/dashboard.vue"),
        },
        {
          path: "/category/list",
          component: () => import("@/views/category/list.vue"),
        },
        {
          path: "/category/pub",
          component: () => import("@/views/category/pub.vue"),
        },
      ],
    });
    export default router;
    ​
    ​
  4. 呈现路由组件

    需要在MainLayout的内容区域添加一个RouterView组件,用来动态显示路由匹配到的组件。

  5. 通过手动修改地址栏地址,可以测试路由组件切换效果

2.2 动态渲染菜单

  1. 改造路由数据包

    export const routes = [
      {
        path: "/",
        component: () => import("@/views/dashboard.vue"),
        meta: {
          label: "数据可视化",
          icon: "area-chart-outlined",
        },
      },
      {
        path: "/category",
        component: () => import("@/views/category/index.vue"),
        meta: {
          label: "分类管理",
          icon: "area-chart-outlined",
        },
        children: [
          {
            path: "/category/list",
            component: () => import("@/views/category/list.vue"),
            meta: {
              label: "分类列表",
            },
          },
          {
            path: "/category/pub",
            component: () => import("@/views/category/pub.vue"),
            meta: {
              label: "发布分类",
            },
          },
        ],
      },
    ];
    const router = createRouter({
      history: createWebHashHistory(),
      routes,
    });

  2. 单独拆分侧边菜单组件

    考虑到代码的可维护性,我们将MainLayout中的侧边菜单逻辑交互单独拆分为一个组件

    组件存放位置:/src/layout/components/side-menu.vue

  3. 在side-menu.vue中使用路由数据包动态渲染

    <template>
      <a-layout-sider v-model:collapsed="collapsed" collapsible>
        <div class="logo" />
        <a-menu v-model:selectedKeys="selectedKeys" theme="dark" mode="inline">
          <template v-for="(item, index) in routes" :key="index">
            <a-menu-item v-if="!item.children" :key="item.path">
              <component :is="item.meta!.icon"></component>
              <span>{
       
       { item.meta!.label }}</span>
            </a-menu-item>
            <a-sub-menu v-else :key="index">
              <template #title>
                <span>
                  <component :is="item.meta!.icon"></component>
                  <span>{
       
       { item.meta!.label }}</span>
                </span>
              </template>
              <a-menu-item v-for="child in item.children" :key="child.path">{
       
       {
                item.meta!.label
              }}</a-menu-item>
            </a-sub-menu>
          </template>
        </a-menu>
      </a-layout-sider>
    </template>
    ​
    <script setup lang="ts">
    import { ref } from "vue";
    import { routes } from "@/router";
    const collapsed = ref(false);
    const selectedKeys = ref(["1"]);
    </script>
    ​
    <style scoped></style>
    ​
  4. 通过useRouter触发路由切换

    const router = useRouter();
    const handleMenu = ({ key }: { key: string }) => { //此处的key是由menu组件点击事件提供
      console.log(key);
      router.push(key);
    };

2.3 动态图标及路由跳转

  1. 安装

    yarn add @ant-design/icons-vue

  2. main.ts全局注册

    import * as antIcons from "@ant-design/icons-vue";
    const app = createApp(App);
    // 注册组件
    Object.keys(antIcons).forEach((key: any) => {
      app.component(key, antIcons[key as keyof typeof antIcons]);
    });
    // 添加到全局
    app.config.globalProperties.$antIcons = antIcons;
  3. 动态组件

    <component is="xxx">

2.4 面包屑交互

  1. 封装面包屑组件

    封装面包屑组件

    使用useRoute、结合watch监听,在面包屑组件中监听路由路径的变化

    // src/layout/component/app-breadcrumb.vue
    <template>
      <a-breadcrumb>
        <a-breadcrumb-item>首页</a-breadcrumb-item>
      </a-breadcrumb>
    </template>
    
    <script setup lang="ts">
    import { watch } from "vue";
    import { useRoute } from "vue-router";
    
    const route = useRoute();
    watch(
      route,
      (newValue) => {
        console.log("路由发生变化了", newValue.path);
      }
    );
    </script>
    
    <style scoped></style>
    
  2. 整合面包屑数据包

    封装一个方法函数,将路由数据包,处理为【path--菜单名】的映射格式:

    {

    '/dashboard':'数据统计',

    '/category':'分类管理',

    '/category/list':'分类列表'

    ......

    }</

### VueVite 前端开发解决方案 Vite 是一种现代前端构建工具,专为快速开发而设计。它利用原生 ES 模块导入来提供更快的冷启动和热更新速度。Vue 是一个流行的渐进式框架,用于构建用户界面。两者结合可以显著提升开发体验。 #### 1. 安装与初始化项目 为了创建基于 VueVite 的新项目,可以通过 `npm` 或 `yarn` 使用官方模板: ```bash npm create vite@latest my-vue-app --template vue cd my-vue-app npm install npm run dev ``` 此命令会生成一个新的 Vue 项目并安装依赖项[^1]。运行 `npm run dev` 启动本地开发服务器。 --- #### 2. 处理静态资源 如果需要在项目中引入图片或其他静态文件,建议将其放置于项目的 `public` 文件夹中。这样可以在 HTML 中通过相对路径访问这些资源,而不必经过打包过程。 例如,在 `index.html` 中引用一张位于 `public/images/logo.png` 的图片: ```html <img src="/images/logo.png" alt="Logo"> ``` --- #### 3. 自定义 Vite 配置 当默认配置无法满足需求时,可自定义 `vite.config.js` 来扩展功能。以下是常见的配置示例: - **设置别名**:简化模块导入路径。 - **优化压缩**:减少生产环境下的包体积。 ```javascript import { defineConfig } from 'vite'; import vue from '@vitejs/plugin-vue'; export default defineConfig({ plugins: [vue()], resolve: { alias: { '@': '/src', // 设置 @ 别名为 /src 路径 }, }, build: { rollupOptions: { output: { manualChunks(id) { if (id.includes('node_modules')) { return id.toString().split('node_modules/')[1].split('/')[0]; } }, }, }, }, }); ``` 以上代码片段展示了如何通过插件支持 Vue 组件解析,并设置了路径别名以便更方便地管理大型项目结构。 --- #### 4. Vue 开发基础 对于初学者来说,理解 Vue 的核心概念至关重要,比如响应式数据绑定、组件化架构以及生命周期钩子函数等[^2]。下面是一个简单的 Vue 组件例子: ```vue <template> <div class="greeting">Hello, {{ name }}!</div> </template> <script> export default { data() { return { name: 'World', }; }, }; </script> <style scoped> .greeting { color: blue; } </style> ``` 该组件展示了一个动态问候语,并应用了局部样式[^2]。 --- #### 5. 生产部署准备 完成开发后,执行以下命令生成适用于生产的静态文件: ```bash npm run build ``` 生成的文件会被存储在指定目录(通常是 `dist`),可以直接上传至任何静态托管服务提供商处。 --- ### 总结 通过合理配置 Vite 并掌握 Vue 的基础知识,开发者能够高效搭建现代化 Web 应用程序。无论是处理静态资产还是实现复杂交互逻辑,这套组合都提供了强大的技术支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值