From 4369d90d98bcf8e27fe3cd352905105d7bb6f992 Mon Sep 17 00:00:00 2001 From: JDS-JH <70583849+JDS-JH@users.noreply.github.com> Date: Fri, 19 Aug 2022 22:05:26 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=80=82=E9=85=8D=20HTML5=20=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=E8=B7=AF=E7=94=B1=20(#11)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 适配 HTML5 模式路由 --- .env.development | 4 ++++ .env.production | 4 ++++ .env.staging | 4 ++++ src/router/index.ts | 7 +++++-- types/env.d.ts | 2 ++ vite.config.ts | 2 +- 6 files changed, 20 insertions(+), 3 deletions(-) diff --git a/.env.development b/.env.development index da355f21..c3d3d7f3 100644 --- a/.env.development +++ b/.env.development @@ -3,3 +3,7 @@ NODE_ENV = development # 自定义的环境变量可以修改(命名必须以 VITE_ 开头) VITE_BASE_API = 'https://vue-typescript-admin-mock-server-armour.vercel.app/mock-api/v1' +# 路由模式 hash 或 html5 +VITE_ROUTER_HISTORY = 'hash' +# 打包路径(网站前缀) +VITE_PUBLIC_PATH = '/' \ No newline at end of file diff --git a/.env.production b/.env.production index 649f4750..cedbc6ec 100644 --- a/.env.production +++ b/.env.production @@ -3,3 +3,7 @@ NODE_ENV = production # 自定义的环境变量可以修改(命名必须以 VITE_ 开头) VITE_BASE_API = 'https://vue-typescript-admin-mock-server-armour.vercel.app/mock-api/v1' +# 路由模式 hash 或 html5 +VITE_ROUTER_HISTORY = 'html5' +# 打包路径(网站前缀) +VITE_PUBLIC_PATH = '/' \ No newline at end of file diff --git a/.env.staging b/.env.staging index 649f4750..cedbc6ec 100644 --- a/.env.staging +++ b/.env.staging @@ -3,3 +3,7 @@ NODE_ENV = production # 自定义的环境变量可以修改(命名必须以 VITE_ 开头) VITE_BASE_API = 'https://vue-typescript-admin-mock-server-armour.vercel.app/mock-api/v1' +# 路由模式 hash 或 html5 +VITE_ROUTER_HISTORY = 'html5' +# 打包路径(网站前缀) +VITE_PUBLIC_PATH = '/' \ No newline at end of file diff --git a/src/router/index.ts b/src/router/index.ts index da4e013f..a73d3687 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -1,4 +1,4 @@ -import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router" +import { createRouter, createWebHashHistory, createWebHistory, RouteRecordRaw } from "vue-router" const Layout = () => import("@/layout/index.vue") /** 常驻路由 */ @@ -202,7 +202,10 @@ export const asyncRoutes: Array = [ ] const router = createRouter({ - history: createWebHashHistory(), + history: + import.meta.env.VITE_ROUTER_HISTORY === "hash" + ? createWebHashHistory(import.meta.env.VITE_PUBLIC_PATH) + : createWebHistory(import.meta.env.VITE_PUBLIC_PATH), routes: constantRoutes }) diff --git a/types/env.d.ts b/types/env.d.ts index 73c071b0..79c7e8f7 100644 --- a/types/env.d.ts +++ b/types/env.d.ts @@ -3,4 +3,6 @@ /** 声明 vite 环境变量的类型(如果未声明则默认是 any) */ declare interface ImportMetaEnv { readonly VITE_BASE_API: string + readonly VITE_ROUTER_HISTORY: "hash" | "html5" + readonly VITE_PUBLIC_PATH: string } diff --git a/vite.config.ts b/vite.config.ts index 19ac982a..b71e9a0e 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -8,7 +8,7 @@ import UnoCSS from "unocss/vite" export default (): UserConfigExport => { return { /** 打包时根据实际情况修改 base */ - base: "./", + base: import.meta.env.VITE_PUBLIC_PATH, resolve: { alias: { /** @ 符号指向 src 目录 */