26 lines
708 B
JavaScript
26 lines
708 B
JavaScript
import { useRouter } from 'vue-router';
|
|
|
|
export default function useRedirect() {
|
|
const router = useRouter();
|
|
|
|
const navigate = (data, { customRouteRedirectName, searchText }) => {
|
|
if (customRouteRedirectName)
|
|
return router.push({
|
|
name: customRouteRedirectName,
|
|
params: { id: searchText },
|
|
});
|
|
|
|
const { matched: matches } = router.currentRoute.value;
|
|
const { path } = matches.at(-1);
|
|
|
|
const to =
|
|
data.length === 1
|
|
? path.replace(/\/(list|:id)|-list/, `/${data[0].id}`)
|
|
: path.replace(/:id.*/, '');
|
|
|
|
router.push({ path: to });
|
|
};
|
|
|
|
return { navigate };
|
|
}
|