我将 vue-router 添加到我的项目中,它在基本设置下运行良好。但我想去掉 URL 中的井号 (#)。所以关注the documentation我切换到历史模式。我使用此链接 (<router-link to="/page/4">Open page</router-link>
) 效果很好,但如果我尝试直接访问此页面(刷新),服务器将返回 404 错误屏幕。
我使用 nginx(docker 的 nginx:alpine 图像)这是默认文件:
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错误?
最佳答案
是nginx配置问题 将下面的配置插入到您的 nginx 服务器 block /配置中
error_page 404 /index.html;
关于docker - vue-router 历史模式在刷新时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48484940/