Skip to content

Commit 6e8c4d6

Browse files
committed
feat: add useBack hook and add button for back
1 parent fc35f58 commit 6e8c4d6

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

src/composables/back.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export function useBack() {
2+
const router = useRouter()
3+
4+
function back() {
5+
if (history.length > 1)
6+
router.back()
7+
else
8+
router.replace('/')
9+
}
10+
11+
return { back }
12+
}

src/pages/about.vue

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
<script lang="ts" setup>
1111
const { t } = useI18n()
1212
13+
const { back } = useBack()
14+
1315
const title = computed(() => {
1416
return t('page.about.title')
1517
})
@@ -27,5 +29,9 @@ useHead({
2729
<p>
2830
{{ t('app.description') }}
2931
</p>
32+
<TheButton mx-auto mt-4 @click="back()">
33+
<i i-carbon-return mr-2 />
34+
<span> {{ t('button.back') }}</span>
35+
</TheButton>
3036
</TheCard>
3137
</template>

src/pages/dynamic/[id].vue

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
</route>
99

1010
<script lang="ts" setup>
11+
const { t } = useI18n()
12+
13+
const { back } = useBack()
14+
1115
const route = useRoute()
1216
1317
const title = computed(() => {
@@ -30,5 +34,9 @@ useHead({
3034
https://uvr.esm.is/guide/file-based-routing.html#dynamic-routes
3135
</a>
3236
</p>
37+
<TheButton mx-auto mt-4 @click="back()">
38+
<i i-carbon-return mr-2 />
39+
<span> {{ t('button.back') }}</span>
40+
</TheButton>
3341
</TheCard>
3442
</template>

typings/auto-imports.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ declare global {
155155
const useAsyncQueue: typeof import('@vueuse/core')['useAsyncQueue']
156156
const useAsyncState: typeof import('@vueuse/core')['useAsyncState']
157157
const useAttrs: typeof import('vue')['useAttrs']
158+
const useBack: typeof import('../src/composables/back')['useBack']
158159
const useBase64: typeof import('@vueuse/core')['useBase64']
159160
const useBattery: typeof import('@vueuse/core')['useBattery']
160161
const useBluetooth: typeof import('@vueuse/core')['useBluetooth']
@@ -493,6 +494,7 @@ declare module 'vue' {
493494
readonly useAsyncQueue: UnwrapRef<typeof import('@vueuse/core')['useAsyncQueue']>
494495
readonly useAsyncState: UnwrapRef<typeof import('@vueuse/core')['useAsyncState']>
495496
readonly useAttrs: UnwrapRef<typeof import('vue')['useAttrs']>
497+
readonly useBack: UnwrapRef<typeof import('../src/composables/back')['useBack']>
496498
readonly useBase64: UnwrapRef<typeof import('@vueuse/core')['useBase64']>
497499
readonly useBattery: UnwrapRef<typeof import('@vueuse/core')['useBattery']>
498500
readonly useBluetooth: UnwrapRef<typeof import('@vueuse/core')['useBluetooth']>

0 commit comments

Comments
 (0)