我可以重定向到不同的路线,但如果我刷新浏览器,该路线将不起作用 [英] I can redirect to a different route, but that route doesn't work if I refresh the browser

查看:25
本文介绍了我可以重定向到不同的路线,但如果我刷新浏览器,该路线将不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过$router.push({ path:/week/${year}/${month}/${day}/}) 我可以成功路由/move(不确定此处使用什么术语)到不同的路由,URL 正确更新.

Through $router.push({ path:/week/${year}/${month}/${day}/}) I can successfully route/move (not sure about what term to use here) to a different route, with the URL correctly updating.

但是一旦出现,如果我刷新浏览器,路由组件就会变空(并且没有控制台错误).

However once there, if I refresh the browser, the routed component becomes empty (and no console error).

知道这里发生了什么吗?

Any idea what's going on here?

这是我的路由器代码:

let router = new Router({
  mode: 'history',
  routes: [
    ...
    {
      path: '/',
      redirect: '/home',
      meta: {
        requiresAuth: true
      }
    },
    {
      path: '/week/:year/:month/:day',
      component: Home,
      meta: {
        requiresAuth: true
      }
    },
    ...
  ]
})

推荐答案

在使用历史模式时,您需要配置用于处理路由器的任何服务器.

You need to configure whichever server that you are using to handle the router as you are using history mode.

我将发布 ApacheNginx 的示例配置,因为它们是最常用的.

I will post the sample configuration for Apache and Nginx since they are most commonly used.

阿帕奇

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.html [L]
</IfModule>

<小时>

Nginx

location / {
  try_files $uri $uri/ /index.html;
}

<小时>

其他信息可以在官方文档中找到.

这篇关于我可以重定向到不同的路线,但如果我刷新浏览器,该路线将不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆