vue-router#useRouter TypeScript Examples

The following examples show how to use vue-router#useRouter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: useTitle.ts    From vite-vue3-ts with MIT License 6 votes vote down vote up
/**
 * Listening to page changes and dynamically changing site titles
 */
export function useTitle() {
  const { currentRoute } = useRouter();

  const pageTitle = usePageTitle();

  watch(
    [() => currentRoute.value.path],
    () => {
      const route = unref(currentRoute);

      const tTitle = route?.meta?.title as string;
      pageTitle.value = tTitle;
    },
    { immediate: true },
  );
}
Example #2
Source File: index.ts    From code996 with The Unlicense 6 votes vote down vote up
/**
 * 获取路由元信息
 */
export function getRoutesMeta() {
  const router = useRouter()
  const { query } = router.currentRoute.value

  const hourData = parseResult(query.hour as string)
  const weekData = parseWeekData(parseResult(query.week as string))
  const timeRange = (query.time as string).split('_')
  const timeStr = `${timeRange[0]}${timeRange[1]}`
  const totalCount = hourData.reduce((total, item) => total + item.count, 0)

  return {
    hourData,
    weekData,
    timeRange,
    timeStr,
    totalCount,
  }
}
Example #3
Source File: url-helper.ts    From code996 with The Unlicense 6 votes vote down vote up
/**
 * 检查路由参数是否合法并跳转
 */
export function checkUrlQueryAndRedirect(): void {
  const router = useRouter()
  const { query } = router.currentRoute.value

  if (!query.time || !query.hour || !query.week) {
    router.push({
      name: 'index',
      query: {
        error: 'url_query_error',
      },
    })
  }
}