hookehuyr

fix(login): fix back button issue

......@@ -38,6 +38,7 @@ import IconFont from '@/components/icons/IconFont.vue'
* @property {String} [background] - Custom background style (CSS value)
* @property {String} [textColor] - Custom text/icon color (default #fff)
* @property {Boolean} [overlay] - Whether to overlay content (no placeholder)
* @property {Boolean} [preventDefaultBack] - Whether to prevent default navigateBack when @back is emitted
*/
const props = defineProps({
title: {
......@@ -59,9 +60,15 @@ const props = defineProps({
overlay: {
type: Boolean,
default: false
},
preventDefaultBack: {
type: Boolean,
default: false
}
})
const emit = defineEmits(['back'])
const canGoBack = ref(false)
/**
......@@ -77,6 +84,14 @@ onMounted(() => {
})
const goBack = () => {
Taro.navigateBack()
// 触发 back 事件
// 父组件可以通过 @back 自定义返回行为
// 例如:登录页可以跳转到首页而非返回上一页
emit('back')
// 如果没有阻止默认行为,执行默认的 navigateBack
if (!props.preventDefaultBack) {
Taro.navigateBack()
}
}
</script>
......
<template>
<view class="min-h-screen bg-white flex flex-col">
<!-- Header -->
<NavHeader title="" :show-back="true" />
<NavHeader title="" :show-back="true" :prevent-default-back="true" @back="handleBack" />
<view class="flex-1 flex flex-col px-[48rpx] pt-[120rpx]">
<!-- Logo/Title -->
......@@ -80,6 +80,20 @@ const form = reactive({
})
/**
* Handle back button click
* - 清空 router store 中保存的路径
* - 跳转到首页(因为导航栈已被 401 清空)
*/
const handleBack = () => {
// 清空保存的路径
const store = routerStore()
store.remove()
// 跳转到首页
Taro.reLaunch({ url: '/pages/index/index' })
}
/**
* Handle login action
*/
const handleLogin = async () => {
......