简体   繁体   English

外部托管时VueJS不起作用

[英]VueJS not working when hosted externally

I've been developing a little app in VueJS, and it seemed to be working fine while developing with a local webserver. 我一直在用VueJS开发一个小应用程序,并且在使用本地Web服务器进行开发时似乎运行良好。 But somehow it doesn't load at all when I deploy it to Github pages. 但是当我将它部署到Github页面时,它根本无法加载。 I've also tried deploying it on my own server, that doesn't work either. 我也曾尝试将其部署在自己的服务器上,但这也不起作用。

The Javascript code executes fine, but somehow the Vue code doesn't. Javascript代码可以很好地执行,但是Vue代码却不能。

Link to the website where I've deployed it 链接到我部署它的网站

Github repository Github仓库

If you are using the default vuejs/vue-router setup and you are publishing to a subfolder, in your case: http://example.com/folder/ , vue-router has an an option to modify the base . 如果您使用默认的vuejs / vue-router设置并且要发布到子文件夹,则您的情况为: http://example.com/folder/ : http://example.com/folder/可以选择修改base

import Vue from 'vue'

import Router from 'vue-router'

import Home from '@/components/Home'

Vue.use(Router)

const routes = [
 { path: '/', name: 'Home', component: Home }
]

export default new Router({
 routes,
 mode: 'history',
 base: '/folder'
})

I've found out the problem, it has to do with routing on my specific setup. 我发现了问题,这与我的特定设置上的路由有关。

When Vue's router has routes defined on the server, it will use the routes' path that has been given. 当Vue的路由器在服务器上定义了路由时,它将使用已给定的路由路径。 My problem was using Vue router with the application being deployed in a folder. 我的问题是将Vue路由器与部署在文件夹中的应用程序一起使用。 So when you execute the app, the current path will point to http://example.com/folder/ while your route points to http://example.com/ in my case. 因此,在您执行该应用程序时,在我的情况下,当前路径将指向http://example.com/folder/,而您的路线则指向http://example.com/

I solved the problem by defining an alternate route should another fail for some reason. 我通过定义一条备用路由解决了这个问题,如果另一条路由由于某种原因失败了。

routes: [
    { path: '/', name: "CollectionMarker", component: CollectionMarker },
    { path: '/cars', name: "CarMarker", component: CarsComponent },
    // This is the path that you need to include, it will redirect when no route has been found
    { path: "*", redirect: "/" }
]

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

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