vue-router#createRouter TypeScript Examples

The following examples show how to use vue-router#createRouter. 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: router.ts    From elenext with MIT License 6 votes vote down vote up
router = createRouter({
  history: createWebHistory(),
  strict: true,
  routes: [
    { path: '/home', redirect: '/' },
    {
      path: '/',
      name: 'Layout',
      component: AppLayout,
      redirect: '/grid',
      children: menus.reduce((prev, item) => {
        const _routes = item.items.map(i => {
          return {
            path: `/${i.name.toLowerCase()}`,
            name: i.name,
            component: i.component,
          }
        })
        prev.push(..._routes)
        return prev
      }, [] as RouteRecordRaw[]),
    },
  ],
})
Example #2
Source File: index.ts    From theila with Mozilla Public License 2.0 6 votes vote down vote up
router = createRouter({
  history: createWebHistory(),
  routes,
})
Example #3
Source File: router.ts    From image-optimizer with MIT License 6 votes vote down vote up
router = createRouter({
  linkActiveClass: 'active',
  history,
  routes: [
    {
      path: '/',
      component: () => import('./views/Main.vue')
    },
    {
      path: '/settings',
      meta: { title: 'Test page' },
      component: () => import('./views/Settings.vue')
    }

  ]
})
Example #4
Source File: main.ts    From formkit with MIT License 6 votes vote down vote up
router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      component: BasicForm,
    },
    {
      path: '/basic-form',
      component: BasicForm,
    },
    {
      path: '/custom-input',
      component: CustomInput,
    },
    {
      path: '/file-upload',
      component: FileUpload,
    },
    {
      path: '/group',
      component: GroupInput,
    },
    {
      path: '/tsx',
      component: TSXExample,
    },
    {
      path: '/plugin-schema',
      component: ModifySchema,
    },
  ],
})
Example #5
Source File: index.ts    From code996 with The Unlicense 6 votes vote down vote up
router = createRouter({
  history: createWebHashHistory('/'),
  routes: [
    {
      path: '/',
      name: 'index',
      component: Intro,
      meta: { title: 'code996' },
    },
    {
      path: '/result',
      name: 'result',
      component: Result,
      meta: { title: 'result | code996' },
    },
  ],
})
Example #6
Source File: entry-server.ts    From vite-ssr with MIT License 5 votes vote down vote up
viteSSR: SsrHandler = function viteSSR(
  App,
  {
    routes,
    base,
    routerOptions = {},
    pageProps = { passToPage: true },
    ...options
  },
  hook
) {
  if (pageProps && pageProps.passToPage) {
    addPagePropsGetterToRoutes(routes)
  }

  return coreViteSSR(options, async (context, { isRedirect, ...extra }) => {
    const app = createApp(App)

    const routeBase = base && withoutSuffix(base(context), '/')
    const router = createRouter({
      ...routerOptions,
      history: createMemoryHistory(routeBase),
      routes: routes as RouteRecordRaw[],
    })

    router.beforeEach((to) => {
      to.meta.state = extra.initialState || null
    })

    provideContext(app, context)

    const fullPath = getFullPath(context.url, routeBase)

    const { head } =
      (hook &&
        (await hook({
          app,
          router,
          initialRoute: router.resolve(fullPath),
          ...context,
        }))) ||
      {}

    app.use(router)
    router.push(fullPath)

    await router.isReady()

    if (isRedirect()) return {}

    Object.assign(
      context.initialState || {},
      (router.currentRoute.value.meta || {}).state || {}
    )

    const body = await renderToString(app, context)

    if (isRedirect()) return {}

    const {
      headTags = '',
      htmlAttrs = '',
      bodyAttrs = '',
    } = head ? renderHeadToString(head) : {}

    return { body, headTags, htmlAttrs, bodyAttrs }
  })
}
Example #7
Source File: main.ts    From vue-components-lib-seed with MIT License 5 votes vote down vote up
router = createRouter({
  history: createWebHashHistory(),
  routes,
})
Example #8
Source File: main.ts    From ncov with MIT License 5 votes vote down vote up
router = createRouter({
    history: createWebHistory(),
    routes
})
Example #9
Source File: index.ts    From vite-plugin-monaco-editor with MIT License 5 votes vote down vote up
router = createRouter({
  history: createWebHistory(),
  routes,
})
Example #10
Source File: index.ts    From vue3-cesium-typescript-start-up-template with MIT License 5 votes vote down vote up
router = createRouter({
  history: createWebHashHistory(),
  // history: createWebHistory(),
  routes,
})
Example #11
Source File: index.ts    From elec-sqlite-vue with GNU General Public License v3.0 5 votes vote down vote up
router = createRouter({
  history: createWebHashHistory(),
  routes
})
Example #12
Source File: index.ts    From gitmars with GNU General Public License v3.0 5 votes vote down vote up
router = createRouter({
    history: createWebHashHistory(),
    // @ts-expect-error
    routes
})
Example #13
Source File: index.ts    From Vue3-Vite-Vuetify3-Typescript-Template with MIT License 5 votes vote down vote up
router = createRouter({
  history: createWebHistory(),
  routes,
})
Example #14
Source File: router.ts    From vue-i18n-next with MIT License 5 votes vote down vote up
export function setupRouter(i18n: I18n): Router {
  const locale = getLocale(i18n)

  // setup routes
  const routes: RouteRecordRaw[] = [
    {
      path: '/:locale/',
      name: 'home',
      component: Home
    },
    {
      path: '/:locale/about',
      name: 'about',
      component: About
    },
    {
      path: '/:pathMatch(.*)*',
      redirect: () => `/${locale}`
    }
  ]

  // create router instance
  const router = createRouter({
    history: createWebHistory(),
    routes
  })

  // navigation guards
  router.beforeEach(async to => {
    const paramsLocale = to.params.locale as string

    // use locale if paramsLocale is not in SUPPORT_LOCALES
    if (!SUPPORT_LOCALES.includes(paramsLocale)) {
      return `/${locale}`
    }

    // load locale messages
    if (!i18n.global.availableLocales.includes(paramsLocale)) {
      await loadLocaleMessages(i18n, paramsLocale)
    }

    // set i18n language
    setI18nLanguage(i18n, paramsLocale)
  })

  return router
}
Example #15
Source File: index.ts    From vue3-ts-base with MIT License 5 votes vote down vote up
router = createRouter({
  history: createWebHashHistory(),
  routes
})
Example #16
Source File: router.ts    From hexon with GNU General Public License v3.0 5 votes vote down vote up
router = createRouter({ history: createWebHashHistory(), routes })
Example #17
Source File: router.ts    From hexon with GNU General Public License v3.0 5 votes vote down vote up
router = createRouter({ history: createWebHashHistory(), routes })
Example #18
Source File: main.ts    From vueconf-london with MIT License 5 votes vote down vote up
router = createRouter({
  history: createWebHistory(),
  routes,
})
Example #19
Source File: entry-client.ts    From vite-ssr with MIT License 5 votes vote down vote up
viteSSR: ClientHandler = async function viteSSR(
  App,
  {
    routes,
    base,
    routerOptions = {},
    pageProps = { passToPage: true },
    debug = {},
    ...options
  },
  hook
) {
  if (pageProps && pageProps.passToPage) {
    addPagePropsGetterToRoutes(routes)
  }

  const app = createSSRApp(App)

  const url = new URL(window.location.href)
  const routeBase = base && withoutSuffix(base({ url }), '/')
  const router = createRouter({
    ...routerOptions,
    history: createWebHistory(routeBase),
    routes: routes as RouteRecordRaw[],
  })

  const context: Context = await createClientContext({
    ...options,
    url,
    spaRedirect: (location) => router.push(location),
  })

  provideContext(app, context)

  let entryRoutePath: string | undefined
  let isFirstRoute = true
  router.beforeEach((to) => {
    if (isFirstRoute || (entryRoutePath && entryRoutePath === to.path)) {
      // The first route is rendered in the server and its state is provided globally.
      isFirstRoute = false
      entryRoutePath = to.path
      to.meta.state = context.initialState
    }
  })

  if (hook) {
    await hook({
      app,
      router,
      initialRoute: router.resolve(getFullPath(url, routeBase)),
      ...context,
    })
  }

  app.use(router)

  if (debug.mount !== false) {
    // this will hydrate the app
    await router.isReady()
    // @ts-ignore
    app.mount(`#${__CONTAINER_ID__}`, true)
    // it is possible to debug differences of SSR / Hydrated app state
    // by adding a timeout between rendering the SSR version and hydrating it later
    // window.setTimeout(() => {
    //   console.log('The app has now hydrated');
    //   router.isReady().then(() => {
    //     app.mount('#app', true);
    //   });
    // }, 5000);
  }
}
Example #20
Source File: router.ts    From malagu with MIT License 5 votes vote down vote up
router = createRouter({
  history: createWebHashHistory(),
  routes,
})
Example #21
Source File: router.ts    From nautilus-wallet with MIT License 5 votes vote down vote up
router = createRouter({
  history: createWebHashHistory(),
  routes
})
Example #22
Source File: index.ts    From project_nodeSnap with GNU General Public License v3.0 5 votes vote down vote up
router = createRouter({
    history: createWebHashHistory(),
    routes
})
Example #23
Source File: index.ts    From master-frontend-lemoncode with MIT License 5 votes vote down vote up
router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
})
Example #24
Source File: index.ts    From master-frontend-lemoncode with MIT License 5 votes vote down vote up
router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
})
Example #25
Source File: index.ts    From master-frontend-lemoncode with MIT License 5 votes vote down vote up
router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
})
Example #26
Source File: index.ts    From master-frontend-lemoncode with MIT License 5 votes vote down vote up
router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
})
Example #27
Source File: index.ts    From master-frontend-lemoncode with MIT License 5 votes vote down vote up
router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
})
Example #28
Source File: index.ts    From vite-vue3-ts with MIT License 5 votes vote down vote up
router = createRouter({
  // 解决 二级路径存在时,路径地址路由不匹配的问题
  // https://juejin.cn/post/7051826951463370760#heading-27
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
  strict: true,
  scrollBehavior: () => ({ left: 0, top: 0 }),
})
Example #29
Source File: index.ts    From rabbit-ui with MIT License 4 votes vote down vote up
router = createRouter({
  history: routerHistory,
  routes: [
    {
      path: '/',
      meta: {
        title: 'Rabbit UI - 一个基于 JavaScript 的简洁 UI 组件库'
      },
      component: () => import('@/App.vue')
    },
    {
      path: '/docs',
      redirect: '/'
    },
    {
      path: '/components',
      redirect: '/'
    },
    {
      path: '/docs/introduce',
      meta: {
        title: '介绍'
      },
      component: () => import('pages/app/views/Introduce.vue')
    },
    {
      path: '/docs/install',
      meta: {
        title: '安装'
      },
      component: () => import('pages/app/views/Install.vue')
    },
    {
      path: '/docs/start',
      meta: {
        title: '快速上手'
      },
      component: () => import('pages/app/views/Start.vue')
    },
    {
      path: '/docs/update',
      meta: {
        title: '更新日志'
      },
      component: () => import('pages/app/views/Update.vue')
    },
    {
      path: '/docs/sponsor',
      meta: {
        title: '支持我们'
      },
      component: () => import('pages/app/views/Sponsor.vue')
    },
    {
      path: '/docs/faq',
      meta: {
        title: '常见问题'
      },
      component: () => import('pages/app/views/FAQ.vue')
    },
    {
      path: '/components/affix',
      meta: {
        title: '图钉'
      },
      component: () => import('pages/app/views/Affix.vue')
    },
    {
      path: '/components/alert',
      meta: {
        title: '警告提示'
      },
      component: () => import('pages/app/views/Alert.vue')
    },
    {
      path: '/components/avatar',
      meta: {
        title: '头像'
      },
      component: () => import('pages/app/views/Avatar.vue')
    },
    {
      path: '/components/back-top',
      meta: {
        title: '返回顶部'
      },
      component: () => import('pages/app/views/BackTop.vue')
    },
    {
      path: '/components/badge',
      meta: {
        title: '徽标'
      },
      component: () => import('pages/app/views/Badge.vue')
    },
    {
      path: '/components/breadcrumb',
      meta: {
        title: '面包屑'
      },
      component: () => import('pages/app/views/Breadcrumb.vue')
    },
    {
      path: '/components/button',
      meta: {
        title: '按钮'
      },
      component: () => import('pages/app/views/Button.vue')
    },
    {
      path: '/components/card',
      meta: {
        title: '卡片'
      },
      component: () => import('pages/app/views/Card.vue')
    },
    {
      path: '/components/carousel',
      meta: {
        title: '走马灯'
      },
      component: () => import('pages/app/views/Carousel.vue')
    },
    {
      path: '/components/checkbox',
      meta: {
        title: '复选框'
      },
      component: () => import('pages/app/views/Checkbox.vue')
    },
    {
      path: '/components/circle',
      meta: {
        title: '进度坏'
      },
      component: () => import('pages/app/views/Circle.vue')
    },
    {
      path: '/components/collapse',
      meta: {
        title: '折叠面板'
      },
      component: () => import('pages/app/views/Collapse.vue')
    },
    {
      path: '/components/color',
      meta: {
        title: '颜色'
      },
      component: () => import('pages/app/views/Color.vue')
    },
    {
      path: '/components/count-down',
      meta: {
        title: '倒计时'
      },
      component: () => import('pages/app/views/CountDown.vue')
    },
    {
      path: '/components/divider',
      meta: {
        title: '分割线'
      },
      component: () => import('pages/app/views/Divider.vue')
    },
    {
      path: '/components/drawer',
      meta: {
        title: '抽屉'
      },
      component: () => import('pages/app/views/Drawer.vue')
    },
    {
      path: '/components/dropdown',
      meta: {
        title: '下拉菜单'
      },
      component: () => import('pages/app/views/Dropdown.vue')
    },
    {
      path: '/components/empty',
      meta: {
        title: '空状态'
      },
      component: () => import('pages/app/views/Empty.vue')
    },
    {
      path: '/components/icon',
      meta: {
        title: '图标'
      },
      component: () => import('pages/app/views/Icon.vue')
    },
    {
      path: '/components/input-number',
      meta: {
        title: '数字输入框'
      },
      component: () => import('pages/app/views/InputNumber.vue')
    },
    {
      path: '/components/jumbotron',
      meta: {
        title: '巨幕'
      },
      component: () => import('pages/app/views/Jumbotron.vue')
    },
    {
      path: '/components/loading-bar',
      meta: {
        title: '加载进度条'
      },
      component: () => import('pages/app/views/LoadingBar.vue')
    },
    {
      path: '/components/message',
      meta: {
        title: '消息提示'
      },
      component: () => import('pages/app/views/Message.vue')
    },
    {
      path: '/components/mini-modal',
      meta: {
        title: '迷你模态框'
      },
      component: () => import('pages/app/views/MiniModal.vue')
    },
    {
      path: '/components/modal',
      meta: {
        title: '模态框'
      },
      component: () => import('pages/app/views/Modal.vue')
    },
    {
      path: '/components/notice',
      meta: {
        title: '通知提醒'
      },
      component: () => import('pages/app/views/Notice.vue')
    },
    {
      path: '/components/page-header',
      meta: {
        title: '页头'
      },
      component: () => import('pages/app/views/PageHeader.vue')
    },
    {
      path: '/components/poptip',
      meta: {
        title: '气泡提示'
      },
      component: () => import('pages/app/views/Poptip.vue')
    },
    {
      path: '/components/progress',
      meta: {
        title: '进度条'
      },
      component: () => import('pages/app/views/Progress.vue')
    },
    {
      path: '/components/radio',
      meta: {
        title: '单选框'
      },
      component: () => import('pages/app/views/Radio.vue')
    },
    {
      path: '/components/result',
      meta: {
        title: '结果'
      },
      component: () => import('pages/app/views/Result.vue')
    },
    {
      path: '/components/skeleton',
      meta: {
        title: '骨架屏'
      },
      component: () => import('pages/app/views/Skeleton.vue')
    },
    {
      path: '/components/spin',
      meta: {
        title: '加载中'
      },
      component: () => import('pages/app/views/Spin.vue')
    },
    {
      path: '/components/steps',
      meta: {
        title: '步骤条'
      },
      component: () => import('pages/app/views/Steps.vue')
    },
    {
      path: '/components/switch',
      meta: {
        title: '开关'
      },
      component: () => import('pages/app/views/Switch.vue')
    },
    {
      path: '/components/tabs',
      meta: {
        title: '选项卡'
      },
      component: () => import('pages/app/views/Tabs.vue')
    },
    {
      path: '/components/tag',
      meta: {
        title: '标签'
      },
      component: () => import('pages/app/views/Tag.vue')
    },
    {
      path: '/components/time',
      meta: {
        title: '相对时间'
      },
      component: () => import('pages/app/views/Time.vue')
    },
    {
      path: '/components/timeline',
      meta: {
        title: '时间轴'
      },
      component: () => import('pages/app/views/Timeline.vue')
    },
    {
      path: '/components/tooltip',
      meta: {
        title: '文字提示'
      },
      component: () => import('pages/app/views/Tooltip.vue')
    }
  ]
})