美文网首页
关于vue history模式下遇到的坑

关于vue history模式下遇到的坑

作者: 用技术改变世界 | 来源:发表于2021-07-14 10:46 被阅读0次

背景: vue-cli4 配置vue-router 为history模式 , 跳转子路由后页面正常,但是刷新页面访问不到了

原因: 因为一般vue脚手架的默认路由配置时hash模式,为了解决直接打开打包文件页面空白的问题,会在config>index.js 下,把build的assetsPublicPath:'/',改成assetsPublicPath:'./';

但变成history路由模式时,就要改回来

解决方案:

1. 设置 assetsPublicPath:'/',

2.  (可无)

devServer: {

    open: true,

    host: 'localhost',

    port: 8080,

    https: false,

    hotOnly: false,   

    // http 代理配置

    proxy: {      

      '/api': {

        target: 'http://127.0.0.1:3000/api',

        changeOrigin: true,

        pathRewrite: {          

            '^/api': ''

        }

      }

    },

    historyApiFallback: true,  /

    before: (app) => {}

  }, 

相关文章

网友评论

      本文标题:关于vue history模式下遇到的坑

      本文链接:https://www.haomeiwen.com/subject/pzfypltx.html