简体   繁体   English

由于相对路径错误,在 Apache 网络服务器上构建后部署 Vue 项目时出现问题

[英]Problems deploying Vue project after build on Apache webserver due to wrong relative paths

I have a simple Vue.js project.我有一个简单的 Vue.js 项目。 I ran the command yarn build , then copied all the files inside dist/ folder onto Apache webserver.我运行命令yarn build ,然后将 dist/ 文件夹中的所有文件复制到 Apache 网络服务器上。

When my URL pointed to the generated index.html, nothing appeared.当我的URL指向生成的index.html时,什么也没有出现。 I discovered this was due index.html pointing to the wrong relative paths that contain the js and CSS files.我发现这是由于 index.html 指向包含 js 和 CSS 文件的错误相对路径。

For example, I noticed this wrong relative inside index.html <script type=module src=/js/app.4dcb0a0b.js></script> The correct one should be <script type=module src=./js/app.4dcb0a0b.js></script> It should be ./js and not /js .例如,我在 index.html 中注意到这个错误的 relative <script type=module src=/js/app.4dcb0a0b.js></script>正确的应该是<script type=module src=./js/app.4dcb0a0b.js></script>应该是./js而不是/js

After I corrected the relative paths, index.html appear correctly.更正相对路径后,index.html 正确显示。

It is tedious if I have to manually change the wrong relative paths every time.如果我每次都必须手动更改错误的相对路径,这很乏味。 How can I have a better solution?我怎样才能有更好的解决方案?

I am using @vue/cli 5.0.1, yarn v1.22.17我正在使用@vue/cli 5.0.1,yarn v1.22.17

I will answer my own question.我会回答我自己的问题。

Credit for the answer goes to this article答案归功于这篇文章

https://medium.com/js-dojo/how-to-solve-vue.js-prod-build-assets-relative-path-problem-71f91138dd79 https://medium.com/js-dojo/how-to-solve-vue.js-prod-build-assets-relative-path-problem-71f91138dd79

Solution is to add the following line to vue.config.js;解决方案是在 vue.config.js 中添加以下行;

module.exports = {publicPath: ''};

I do not know why this solution works.我不知道为什么这个解决方案有效。 If someone knows, please drop some comments.如果有人知道,请留下一些评论。 Thank you.谢谢你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM