Vuejs2 vue路由器、Web包开发服务器和historyApiFallback,/foo可以,但/foo/:bar给出404(/foo/bundle.js)
我有一个关于vue路由器的问题。我怎样才能完成这样的路线:Vuejs2 vue路由器、Web包开发服务器和historyApiFallback,/foo可以,但/foo/:bar给出404(/foo/bundle.js),vuejs2,webpack-dev-server,vue-router,Vuejs2,Webpack Dev Server,Vue Router,我有一个关于vue路由器的问题。我怎样才能完成这样的路线: domain.com/confirm/randomstring randomString是一个标记,显示在此模板中: const ConfirmComp = { template: `<div>ConfirmComp <img src="img/logo.svg"> <h3>you confirm: {{ $route.params.token }} </h3>
domain.com/confirm/randomstring
randomString是一个标记,显示在此模板中:
const ConfirmComp = {
template: `<div>ConfirmComp
<img src="img/logo.svg">
<h3>you confirm: {{ $route.params.token }} </h3>
</div>`
};
网页包devServer配置:
export default new VueRouter({
mode: 'history',
base: "/",
routes: [
{path: "/", component: Root},
{path: "/suscribe", component: InscriptionComp},
{path: "/confirm/:token", component: ConfirmComp, props: true}
]
});
conf.devServer = {
host: "localhost",
historyApiFallback: true,
proxy: {
"/api": "http://localhost:8080"
}
}
事情0:在我的服务器上添加historyApiFallback有助于在/xxx或/yyy路由上呈现页面刷新,但不使用参数(/yyy/:令牌)
事情1:在页面中单击链接/confirm/r5t4y7进行导航时:
-组件渲染
-参数令牌id显示正确,但
-404在图像上未找到“/confirm/img/logo.svg”(不应包含“/confirm”)
事情二:提神的时候
-什么都不起作用:它尝试加载style.css和bundle.css时使用“/confirm”基而不是/
版本:vue 2.5.16、vue路由器3.0.1、网页包4.6.0、网页包开发服务器3.1.3、vue加载程序15.0.7
谢谢你这不是路由器的问题,而是服务器配置的问题。您应该将所有不存在的URL路径重写为
/index.html
@IVOGELOV这是通过“historyApiFallback:true”完成的,如果完成了,那么您应该能够在浏览器中请求/bla/bla/nothing.css
,并获得您的index.html
。如果你没有得到它-那么服务器没有正确配置。当然,我会看看这个。但是为什么我的应用程序要请求“/xxx/bundle.js”和“xxx/style.css”?这些文件是真实的并且在根目录下。如果服务器提供index.html而不是bundle.js,那么它将不起作用。您是否尝试在路由器链接中的路径前面添加/
?通常,最好使用命名路由并按名称而不是URL引用它们。
conf.devServer = {
host: "localhost",
historyApiFallback: true,
proxy: {
"/api": "http://localhost:8080"
}
}