24 lines
707 B
JavaScript
24 lines
707 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);
|
|
|
|
if (!data.length || data.length > 1)
|
|
return router.push({ path: path.replace(/:id.*/, '') });
|
|
|
|
router.push({ path: path.replace(/\/(list|-list|:id)/, `/${data[0].id}`) });
|
|
};
|
|
|
|
return { navigate };
|
|
}
|