【发布时间】:2018-07-07 04:26:42
【问题描述】:
我将 vue-router 添加到我的项目中,它在基本设置下运行良好。但我想去掉 URL 中的主题标签 (#)。所以跟随the documentation 我切换到历史模式。我使用此链接 (<router-link to="/page/4">Open page</router-link>) 效果很好,但如果我尝试直接访问此页面(刷新),服务器会返回 404 错误屏幕。
我使用 nginx(docker 的 nginx:alpine 映像),这是 default 文件:
server {
root /var/www;
index index.html index.htm;
server_name localhost;
location / {
try_files $uri $uri/ /index.html;
}
}
这是路由器:
new VueRouter({
mode: 'history',
routes: [
{
path: '/page/:id',
component: page
}
],
});
为什么会返回 404 错误?
【问题讨论】:
-
不是一个解决方案,只是一个旁注:
router-link之所以有效,是因为它是一个 pushState,而不是对该 URL 的真正请求(因此它不会发送到服务器)。真正的请求是在您直接导航或刷新时发出的。 -
当使用历史模式时,您需要确保您的服务器返回任何服务器请求的索引页。您需要在服务器端进行配置。
标签: docker nginx vue.js vue-router