vite+Vue3 createWebHistory 路由前缀配置

vue 作为前端最火的前端框架之一,大家在使用的过程中必不可少需要使用router(路由)。

路由有hashhistory 两种,本期主要针对history模式路由前缀 使用和配置,进行简单介绍。

env变量

在根目录下,创建开发、测试、灰度、生产等 env文件

在这里插入图片描述

// .env.dev
VITE_APP_ENV = 'development'
VITE_APP_ROUTER = '/xxx-demo/'

//.env.prod
VITE_APP_ENV = 'prod'
VITE_APP_ROUTER = '/xxx-demo/'

package.json

// 使用 env 文件

{
  "name": "xxx-demo",
  "version": "1.0.0",
  "license": "MIT",
  "scripts": {
    "dev": "vite --mode dev --open",
    "build": "vite build --mode test",
    "prod": "vite build --mode prod"
  },
  "dependencies": {
  ...
  }
}

router路由文件

import { createRouter, createWebHistory } from 'vue-router'
/**
 * 去中心化管理路由
 */
const files = import.meta.globEager('./modules/*.ts')
const modules = Object.keys(files).reduce((e, path: string) => files[path]?.default, [])

const Router = createRouter({
  // VITE_APP_ROUTER  == /xxx-demo/
  // 不需要路由前缀,createWebHistory则不需要传值
  history: createWebHistory(import.meta.env.VITE_APP_ROUTER),
  routes: [...modules]
})
export default Router


路由重置、404配置

// 嵌套路由的模板组件
import Main from '@/views/layouts/mainBox.vue'

const base = [
  {
    path: '/',
    // 默认重置到/index(首页)
    redirect: '/index'
  },
  {
    path: '/page',
    component: Main,
    children:[
      {
        path: '404',
        name: 'pagge404',
        component: () => import(/* webpackChunkName: "error" */ '@/views/layouts/error.vue')
      }
    ]
  },
  {
    path: '/:pathMatch(.*)',
    // 没有匹配的path,则去404页面
    redirect: '/page/404'
  }

]

export default base

ngxin 配置

安装nginx可以参考
nginx 安装1
nginx 安装2

server
{
    listen 80;
	# 域名配置
    server_name  xxx.demo.com;
    index index.html;



    access_log  /var/log/nginx/xxx.demo.com.log main;
    error_log  /var/log/nginx/xxx.demo.com.log.err error;

    location /emao-ui {
      # 项目地址
      alias /data/www/project-xxx-demo/;
       try_files $uri $uri/ @xxx_demo;
       index index.html;
    }

    location @xxx_demoe {
       rewrite ^.*$ /xxx-demo/index.html last;
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值