hash路由转history路由报错Failed to resolve async component default: ChunkLoadError: Loading chunk 28 failed

问题

hash 模式路由改为 history 模式后,路由不能跳转,并报错
在这里插入图片描述

原因

https://cloud.tencent.com/developer/article/1653841

解决

修改webpack配置:
vue.config.js 文件中对基本路径 publicPath 进行修改
之前 publicPath./ ,现在改为 /

/**** 路由配置 ****/
export default new Router({
  scrollBehavior: () => ({
    y: 0
  }),
  // mode: 'hash', // 之前
  mode: 'history', // 现在
  routes: [ ... ]
})

/**** vue.config.js ****/
module.exports = {
  // 基本路径
  // publicPath: './',
  publicPath: '/', // 【主要代码】
  // 输出文件目录
  outputDir: 'superviseAppWeb',
  ...
}

【新问题】刷新后页面404

history模式下刷新页面会出现404的情况

【原因】

在这里插入图片描述

在这里插入图片描述

【解决】

history 模式下刷新出现 404解决

  1. 根据前端发送的请求以及后端已有的资源路径做判断(正则)匹配,区分哪些是前端路由哪些是后端路由。
  2. nginx 中间代理区分前后端路由
  3. node.js提供的中间件Connect
npm instal connect-history-api-fallback
// common.js 规范
    
// 引入 express
const express = require('express')

const history = require('connect-history-api-fallback');

// 新建实例
const app = express()

app.use(history());
 
// 利用中间件 给服务器指定静态资源(打包后的项目文件)
app.use(express.static(__dirname+'/static'))

// 配置后端路由
app.get('/person',(request,response)=>{
	// 给浏览器返回的信息
    response.send({
        name:'tom',age:19
    })
})
// 开启监听
app.listen(5005,(err)=>{
if(!err) conslole.log('服务器启动成功!')
})

404问题解决来源
https://blog.csdn.net/qq_37974755/article/details/125724686
https://blog.csdn.net/weixin_53312997/article/details/127490958
Nginx配置修改 https://www.cnblogs.com/zhangzhijian/p/16896484.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值