jointrashposs/components/g/NuxtLink.vue
2023-11-26 01:48:16 +09:00

44 lines
1.1 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<NuxtLink
:to="realHref"
:href="undefined"
>
<slot></slot>
</NuxtLink>
</template>
<script setup lang="ts">
import { cleanDoubleSlashes, withTrailingSlash } from 'ufo';
import { isLocalPath, sanitizeInternalPath } from '@/assets/js/misc';
import type { RouteLocationRaw } from '#vue-router';
/**
* TrailingSlashをつけているpnpm generate時の出力ディレクトリ構造の関係ので、
* 二重にスラッシュが入って無限ループに陥らないようにするための
* NuxtLinkのラッパーコンポーネント
*/
const rawProps = defineProps<{
to?: RouteLocationRaw | string;
href?: RouteLocationRaw | string;
}>();
const realHref = computed(() => {
const rhf = rawProps.to ?? rawProps.href;
if (rhf && typeof rhf === 'string') {
if (isLocalPath(rhf)) {
return withTrailingSlash(cleanDoubleSlashes(sanitizeInternalPath(rhf)), true);
}
return rhf;
}
// TODO: ルート定義をオブジェクトで渡された時のバリデーション
return rhf;
});
</script>