Deploy Vue to GitHub Pages. Error with vue-router

泄露秘密 提交于 2021-01-27 18:10:48

问题


I was experiencing some trouble while deploying a Vue application build with vue-cli v3.0. to GitHub Pages. I'm using subtree to send the dist folder only to gh-pages branch. First the problem was that the assets where not found but I fixed it using baseUrl on vue.config.js. Now the problem is that the #app element is empty. I found out that if I don't use vue-router (render the view direct instead of using <router-view/>) the app works fine with GitHub pages. I believe there is some issue with the route path option but I'm unable to figure out how to fix it.

The repository with the issue is https://github.com/guizoxxv/vue-cli-deploy and the GitHub Page link is https://guizoxxv.github.io/vue-cli-deploy/

Thank you.


回答1:


I believe I discovered the cause for this issue:

As GitHub Pages URL is not served from the root

https://guizoxxv.github.io/vue-cli-deploy/ has vue-cli-deploy/ after the root /

I need to specify a base option for my app on vue-router options. Here is the documentation https://router.vuejs.org/api/#base




回答2:


Update 2020

baseUrl is Deprecated since Vue CLI 3.3, please use publicPath instead.

If you are using Vue CLI >= 3.3 you might want to check out this vue-cli plugin which automates github pages deployment using github actions.

By default, Vue CLI assumes your app will be deployed at the root of a domain, e.g. https://www.my-app.com/. If your app is deployed at a sub-path, you will need to specify that sub-path using this option. For example, if your app is deployed at https://www.foobar.com/my-app/, set baseUrl to '/my-app/'. So :

  1. Create a new file in root directory of your project and name it ‘vue.config.js’
  2. In ‘vue.config.js’ file paste the following code:

    module.exports = { baseUrl: ‘/my-first-project/’ }

NOTE: in baseUrl inside the // chars you have to put the name of your project.

Read more here

Also, read a full article how to deploy vue.js project in github pages here




回答3:


can you try this in your main.js

new Vue({
    el: '#app',
    router,
    components: { App },
    template: '<App/>',
});


来源:https://stackoverflow.com/questions/50175802/deploy-vue-to-github-pages-error-with-vue-router

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!