【问题标题】:Vue is always redirecting to home page after F5 refresh or when I enter url manuallyF5 刷新或手动输入 url 后,Vue 总是重定向到主页
【发布时间】:2020-11-06 11:19:30
【问题描述】:

我为梦幻足球游戏构建了一个应用程序并将其上传到 FTP。 当我去一个不在家的路线(例如'/cup')并按F5时,应用程序总是将我重定向到主页('/')。 当我手动输入 url (mysite.com/cup) 时也会发生同样的情况,它会将我带到主页。

这是我的路由器:

import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../components/Home/Home.vue";


const Login = () => import("../components/Login/Login");
const Register = () => import("../components/Register/Register");
const UserPage = () => import("../components/UserPage/UserPage");
const Cup = () => import("../components/Cup/Cup");
const H2H = () => import("../components/H2H/H2H");
const Transfers = () => import("../components/Transfers/Transfers");
const RulesAndPrizes = () => import("../components/Rules/Rules");
const GetAllPlayers = () => import("../components/GetAllPlayers");
const tactics = () => import("../components/tactics");
const AllPlayersTable = () => import("../components/AllPlayersTable");
const AdminPanel = () => import("../components/AdminPanel/AdminPanel");
const NotFound = () => import("../components/common/NotFound");

import * as firebase from "firebase/app";
import "firebase/auth";

import getAllUsers from '../utils/getAllUsers'

Vue.use(VueRouter);

const routes = [{
  path: "/",
  name: "Home",
  component: Home,
  meta: { title: 'FFL: Home' },
  beforeEnter(to, from, next) {
    document.title = to.meta.title
    next()
  }
},
{
  path: '/login',
  name: 'login',
  component: Login,
  props: true,
  meta: { title: 'FFL: Login' },
  beforeEnter(to, from, next) {
    firebase.auth().onAuthStateChanged(user => {
      if (user) {
        next('/')
      } else {
        document.title = to.meta.title
        next()
      }
    });
  }
},
{
  path: '/register',
  name: 'register',
  component: Register,
  props: true,
  meta: { title: 'FFL: Register' },
  beforeEnter(to, from, next) {
    firebase.auth().onAuthStateChanged(user => {
      if (user) {
        next('/')
      } else {
        document.title = to.meta.title
        next()
      }
    });
  }
},
{
  path: '/team-details/:id',
  component: UserPage,
  props: true,
  name: "UserPage",
  beforeEnter(to, from, next) {
    document.title = 'My Team'
    next()
  }
},
{
  path: '/transfers/:id',
  component: () =>
    import(/* webpackChunkName: "user-transfers" */ "../components/UserPage/UserTransfers/UserTransfers"),
  name: 'mytransfers',
  props: true,
  meta: { title: 'FFL: Transfers Center ' },
  beforeEnter(to, from, next) {
    firebase.auth().onAuthStateChanged(async user => {
      if (user) {
        const teamName = to.params.id
        const users = await getAllUsers()
        const teamId = Object.values(users).filter(u => {
          return u.userLogo === teamName
        })[0].uid
        if (teamId === user.uid) {
          document.title = to.meta.title + users[teamId].userTeam
          next()
        } else {
          next('/')
        }
      } else {
        next('/')
      }
    });
  }
},
{
  path: '/getallplayers',
  name: 'getallplayers',
  component: GetAllPlayers
},
{
  path: '/tactics',
  name: 'tactics',
  component: tactics
},
{
  path: '/allplayers',
  name: 'allplayers',
  component: AllPlayersTable
},
{
  path: '/admin',
  name: 'admin',
  meta: { title: 'FFL: Admin Panel' },
  component: AdminPanel,
  beforeEnter(to, from, next) {
    document.title = to.meta.title
    next()
  }
},
{
  path: '/cup',
  name: 'cup',
  props: true,
  component: Cup,
  meta: { title: 'FFL: Cup' },
  beforeEnter(to, from, next) {
    document.title = to.meta.title
    next()
  }
},
{
  path: '/h2h',
  name: 'h2h',
  props: true,
  component: H2H,
  meta: { title: 'FFL: H2H League' },
  beforeEnter(to, from, next) {
    document.title = to.meta.title
    next()
  }
},
{
  path: '/transfers',
  name: 'transfers',
  props: true,
  component: Transfers,
  meta: { title: 'FFL: Transfers' },
  beforeEnter(to, from, next) {
    document.title = to.meta.title
    next()
  }
},
{
  path: '/rules-prizes',
  name: 'rules-prizes',
  props: true,
  component: RulesAndPrizes,
  meta: { title: 'FFL: Rules and prizes' },
  beforeEnter(to, from, next) {
    document.title = to.meta.title
    next()
  }
},
{
  path: '*',
  name: 'not-found',
  meta: { title: 'FFL: Page not found' },
  component: NotFound,
  beforeEnter(to, from, next) {
    document.title = to.meta.title
    next()
  }
}
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes
});

export default router;

提前感谢您的帮助!

编辑:这是我的 .htaccess 文件

ErrorDocument 404 http://ff-legends.com/

【问题讨论】:

  • 重定向是做什么的? .ht 访问?可以发一下吗?
  • 嗯,奇怪。它显示“ErrorDocument 404 ff-legends.com”我不记得以某种方式删除或编辑它。我该如何解决这个问题?

标签: javascript apache vue.js redirect vue-router


【解决方案1】:

检查您的.htaccess 文件是否正确。从 Vue 路由器 docs 复制并粘贴此推荐设置:

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

【讨论】:

    猜你喜欢
    • 2020-08-26
    • 2021-12-06
    • 2022-08-22
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    相关资源
    最近更新 更多