vue history模式下,打包后,出现空白页情况

修改config/index.js下的build中的assetsPublicPath: '/' 为 assetsPublicPath: './',
build: {
  // Template for index.html
  index: path.resolve(__dirname, '../dist/index.html'),

  // Paths
  assetsRoot: path.resolve(__dirname, '../dist'),
  assetsSubDirectory: 'static',
//assetsPublicPath: '/' 
  assetsPublicPath: './',

  /**
   * Source Maps
   */

  productionSourceMap: true,
  // https://webpack.js.org/configuration/devtool/#production
  devtool: '#source-map',

  // Gzip off by default as many popular static hosts such as
  // Surge or Netlify already gzip all static assets for you.
  // Before setting to `true`, make sure to:
  // npm install --save-dev compression-webpack-plugin
  productionGzip: false,
  productionGzipExtensions: ['js', 'css'],

  // Run the build command with an extra argument to
  // View the bundle analyzer report after build finishes:
  // `npm run build --report`
  // Set to `true` or `false` to always turn it on or off
  bundleAnalyzerReport: process.env.npm_config_report
}

   vue搭建的项目,路由一直用的hash模式,所以url中都会带有一个“#”号。现在想要去掉“#”,于是使用history模式 { mode: 'history' },
mode: 'history',使用这个模式,在开发阶段一切都是正常的,访问地址是localhost:8888,以上没什么错误,一切都好用。url里也没有#了。可是打包之后,访问项目会发现页面一片空白的情况。不是404,不报错,只是空白!!

解决办法如下:

   此时只需要修改router中的index.js,给路由中加一个base的属性,值为 ‘/Main/’就可以了。如下代码:

export default new Router({
  mode: 'history',
  base: '/Main/',
  routes: [
    {
      path: '/',
      name: 'Main',
      component: Main
    }
  ]
})

 

  记住这个base,base值两边一定要有"/" ,如: '/Main/'

 

     其次,这个文件夹是服务器放项目的文件夹,不是你本地项目的文件夹位置!!

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值