前端——通过Vue打包上线需要注意事项

本文详细介绍了Vue项目的静态资源路径配置,包括在vue.config.js中设置`publicPath`为`./`。同时,文章对比了Vue Router 3.x和4.x中路由模式的差异,3.x使用`mode`设置,而4.x则通过`createWebHashHistory`或`createWebHistory`创建历史模式。重点强调了在使用Vue Router的history模式时,开发环境与生产环境的配置区别,以及hash模式的一致性。对于history模式,生产环境必须配置静态资源路径,而hash模式则在任何环境下都需配置。

1.静态资源路径

在vue.config.js中添加: publicPath: "./",

module.exports = defineConfig({
  //设置静态资源路径,一般是打包上线后用的
  publicPath: "./",
})

2.注意路由配置模式

(1)两种模式:hash、history

(2)vue-router3x中与vue-router4x中的设置方式不一样

vue-router3x中,通过modle设置

modle:hash
modle:history

vue-router4x中,通过createWebHashHistory / createWebHistory


import { createRouter, createWebHistory,createWebHashHistory } from 'vue-router'
const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login/index.vue')
  }
]
const router = createRouter({
  history: createWebHashHistory(process.env.BASE_URL)
  //history:createWebHistory(process.env.BASE_URL)
  routes
})

export default router

Vue-Router 4.x_猫老板的豆的博客-优快云博客_vue-router4.x

3.Vue-router使用history模式,

开发环境下,不能配置静态资源路径;

生产环境下,必须配置静态资源路径;

4.Vue-router使用hash模式

开发环境/生产环境,都需要配置静态资源路径

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值