跳转至

退出问题

对,这里就是导致二级目录部署后跳根路径的元凶

location.href = '/index'

这是一个绝对路径跳转,浏览器会直接去:

https://你的域名/index

而不是

https://你的域名/psy/index

✅ 正确改法(RuoYi-Cloud-Vue3 标准做法)

✔ 推荐方式:使用 Vue Router(最干净)

import router from '@/router'

function logout() {
  ElMessageBox.confirm('确定注销并退出系统吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    userStore.logOut().then(() => {
      router.push('/login')   // 不要再用 location.href
    })
  }).catch(() => {})
}

为什么这样就能自动带 /psy

因为你之前已经改了:

history: createWebHistory(import.meta.env.VITE_APP_BASE_PATH)

所以:

router.push('/login')
实际跳转 = /psy/login

Vue Router 会自动拼接 base。


🧠 为什么不能再用 location.href

写法 是否带 base 结果
router.push('/login') ✅ 自动带 正确
location.href = '/login' ❌ 不带 跳主域名根
location.href = '/index' ❌ 不带 你现在的问题

❓那 /index 还需要吗?

不需要。

RuoYi 登录页路由就是:

/login

退出后回登录页即可,框架会自动处理权限与重定向。


✅ 最终正确版本

import router from '@/router'

function logout() {
  ElMessageBox.confirm('确定注销并退出系统吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    userStore.logOut().then(() => {
      router.push('/login')
    })
  }).catch(() => {})
}

如果你项目里还有:

  • location.href = '/'
  • location.href = '/index'
  • window.open('/login')

都要同样方式替换成 router.push(),否则二级目录部署一定会出问题。

评论