vue的项目编译打包后默认情况下需要有服务器才能打开,那么在不用服务器的情况下怎么打开呢

首先在项目根目录下的vue.config.js(如果没有就自己创建一个)里加一个配置项 publicPath:"./" 或者 publicPath:"" (这个配置项的目的是指明项目在服务器下的路径,这里设置成相对路径)

module.exports={
    publicPath:"./"
}

然后把rutour的历史模式改为 hash模式(因为hash模式不会走服务器,而是在本地直接处理) vue3的项目在 router / index.js 里改

const router = createRouter({
  // history: createWebHistory(process.env.BASE_URL),
  history: createWebHashHistory(),
  routes
})

这里要注意下,createWebHashHistory这个函数记得要导入

import { createRouter, createWebHistory,createWebHashHistory } from 'vue-router'
import Home from '../views/Home.vue'
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐