Vue-cli assets SubDirectory及PublicPath区别详解

2022-07-30,,,,

近期在参与用vue+ springboot前后端不分离项目,遇到了前端打包后dist文件放到后台无法运行报404错误,static下的资源都访问不了问题。

问题1:我们知道前后端不分离项目,一些静态图片、页面直接放在resource/static下,由于前后台分开开发,前端进行了跨域处理,dist文件放到后台就相当于本地静态资源,所以不需要跨域处理,可以将引入跨域的路径baseurl置空

const service = axios.create({
    //baseurl: '/appstore',
    baseurl: '',
    responsetype: 'json',
    timeout: 5000 // request timeout
  })

问题2:就是assetspublicpath的问题,先去分析下assetspublicpath和assetssubdirectory 。

找到config/index.js文件下的build配置改为 assetspublicpath: '/dist/'

build: {
  index: path.resolve(__dirname, '../dist/index.html'),
  // paths
  assetsroot: path.resolve(__dirname, '../dist'),
  assetssubdirectory: 'static',
  // assetspublicpath: '/',  assetspublicpath: '/dist/',
}
  • index:模板
  • assetsroot:打包后文件存放的路径
  • assetssubdirctory:除了index.html之外的静态资源要存放的路径
  • assetspublicpath: 代表打包后,index.html里面引用资源的相对地址

这样配置下就ok了

后台访问时要加上assetspublicpath地址dist,即http://localhost:8080/dist/index.html#

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

《Vue-cli assets SubDirectory及PublicPath区别详解.doc》

下载本文的Word格式文档,以方便收藏与打印。