@kitbag/router
TypeScript icon, indicating that this package has built-in type declarations

0.5.2 • Public • Published

@kitbag/router

Type safe router for Vue.js

NPM Version Netlify Status Discord chat Open in StackBlitz

Getting Started

Get Started with our documentation

Installation

# bun
bun add @kitbag/router
# yarn
yarn add @kitbag/router
# npm
npm install @kitbag/router

Define Basic Routes

Create an array of possible routes. Learn more about defining routes.

// /routes.ts
import { createRoutes } from '@kitbag/router'

const Home = { template: '<div>Home</div>' }
const About = { template: '<div>About</div>' }

export const routes = createRoutes([
  { name: 'home', path: '/', component: Home },
  { name: 'path', path: '/about', component: About },
])

Plugin

Create a router instance and pass it to the app as a plugin

import { createApp } from 'vue'
import { createRouter } from '@kitbag/router'
import { routes } from '/routes'
import App from './App.vue'

const router = createRouter(routes)
const app = createApp(App)

app.use(router)
app.mount('#app')

Update Registered Router

This block utilizes declaration merging to provide the internal types to match the actual router you're using. You put this in main.ts right after you call createRouter, or you can export your router and put this interface inside of a router.d.ts file, anywhere that your tsconfig can find it.

declare module '@kitbag/router' {
  interface Register {
    router: typeof router
  }
}

Push

To navigate to another route, you can use router.push. This method will update the URL for the browser and also add the URL into the history so when a user uses the back button on their browser it will behave as expected.

import { createRoutes, useRouter } from '@kitbag/router'

const routes = createRoutes([
  {
    name: 'user',
    path: '/user',
    component: ...,
    children: createRoutes([
      {
        name: 'profile',
        path: '/profile',
        component: ...,
      },
      {
        name: 'settings',
        path: '/settings',
        component: ...,
      }
    ])
  }
])

const router = useRouter(routes)

router.push('user.settings')

The push method also accepts a plain string if you know the URL you want to go to.

router.push('/user/settings')
router.push('https://github.com/kitbagjs/router')

This source argument is type safe, expecting either a Url or a valid route "key". Url is any string that starts with "http", "https", or a forward slash "/". Route key is a string of route names joined by a period . that lead to a non-disabled route. Additionally if using the route key, push will require params be passed in if there are any.

Update

If you only wish to change the params on the current route you can use router.route.update.

router.route.update('myParam': 123)

or for setting multiple params at once

router.route.update({
  myParam: 123,
  tab: 'github',
})

RouterView

Give your route components a place to be mounted

<!-- App.vue -->
<div class="app">
  ...
  <!-- matched route.component gets rendered here -->
  <router-view />
</div>

This component can be mounted anywhere you want route components to be mounted. Nested routes can also have a nested RouterView which would be responsible for rendering any children that route may have. Read more about nested routes.

RouterLink

Use RouterLink for navigating between routes.

<template>
  ...
  <!-- router-link renders as <a> with href -->
  <router-link :to="(resolve) => resolve('home')">Go somewhere</router-link>
</template>

This component gives the router the power to change the URL without reloading the page.

Readme

Keywords

none

Package Sidebar

Install

npm i @kitbag/router

Weekly Downloads

122

Version

0.5.2

License

none

Unpacked Size

134 kB

Total Files

6

Last publish

Collaborators

  • stackoverfloweth
  • charsh