vue-admin-template地址栏刷新或F5刷新返回404或空白的问题

当使用vue-admin-template并开启history模式时,刷新页面可能出现404或空白。这通常由于后台服务器如nginx或apache未配置支持。为解决此问题,需更新后台服务器配置,或者若后端不支持,可选择禁用history模式。

vue-admin-template刷新的时候会遇到404或空白的问题,这个问题是因为开启了history模式
在这里插入图片描述
但是后台的nginx或apache并没有支持,这时把后台nginx或apache加上就可以了

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

这样就可以了,这部分内容在官网上也有,
如果后端不支持的话,就只能禁用history模式了,把mode屏蔽了就可以了
在这里插入图片描述

<template> <div class="login-container"> <div class="login-box"> <h2>高校教材管理系统</h2> <form @submit.prevent="handleLogin"> <input v-model.number="userId" type="number" placeholder="请输入账号(工号/学号)" required /> <input v-model="password" type="password" placeholder="请输入密码" required /> <button type="submit" :disabled="loading"> {{ loading ? '登录中...' : '登录' }} </button> </form> </div> </div> </template> <script setup> import { ref } from 'vue' import { useRouter } from 'vue-router' import axios from 'axios' const userId = ref(0) const password = ref('') const router = useRouter() const loading = ref(false) const handleLogin = async () => { if (loading.value) return loading.value = true try { const res = await axios.post('http://localhost:8080/api/auth/login', { userId: userId.value, phone: password.value }) console.log('登录返回:', res.data) // 调试时查看 role 是否为预期值 if (res.data.success) { localStorage.setItem('user', JSON.stringify(res.data)) const role = res.data.role if (['admin', 'Admin', '管理员'].includes(role)) { router.push('/admin/textbook') } else { router.push('/user/query') } } else { alert('账号密码错误') } } catch (err) { alert('连接服务器失败,请检查后端是否启动') } finally { loading.value = false } } </script> <style scoped> .login-container { display: flex; justify-content: center; align-items: center; height: 100vh; background: #f0f2f5; } .login-box { width: 400px; padding: 50px; background: white; border-radius: 10px; box-shadow: 0 4px 20px rgba(0,0,0,0.1); text-align: center; } h2 { margin-bottom: 30px; color: #1890ff; font-size: 24px; } input { width: 100%; padding: 12px; margin: 10px 0; border: 1px solid #ccc; border-radius: 6px; font-size: 14px; box-sizing: border-box; } button { width: 100%; padding: 12px; background-color: #1890ff; color: white; border: none; border-radius: 6px; font-size: 16px; cursor: pointer; margin-top: 10px; } button:hover:not(:disabled) { background-color: #40a9ff; } button:disabled { background-color: #ccc; cursor: not-allowed; } </style>
最新发布
12-25
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值