Provide a type safe router to Nuxt with auto-generated typed definitions for route names and autocompletion for route params
- π Provides a hook
useTypedRouterthat returns an alias of$typedRouterand also a typed list of your routes - π Expose a global method
$typedRouter(clone of vue-router), but typed with the routes defined inpagesdirectory - π¦ Provides auto-completion and errors for route params in
pushandreplacemethods
Demo π§ͺ : nuxt-typed-router-demo
- Nuxt 3
- Nuxt 2 (via
nuxt2branch)
yarn add -D nuxt-typed-router
# or
npm install -D nuxt-typed-routerFor Nuxt 2 usage, check out the docs at the nuxt2 branch
yarn add -D nuxt-typed-router@legacy
# or
npm install -D nuxt-typed-router@legacyFirst, register the module in the nuxt.config.ts
import TypedRouter from 'nuxt-typed-router';
export default defineNuxtConfig({
buildModules: [TypedRouter],
nuxtTypedRouter: {
// options
},
});interface ModuleOptions {
/** Output directory where you cant the files to be saved
* (ex: "./models")
* @default "<srcDir>/generated"
*/
outDir?: string;
/** Name of the routesNames object (ex: "routesTree")
* @default "routerPagesNames"
* */
routesObjectName?: string;
}The module will generate 4 files each time you modify the pages folder :
~/<outDir>/__routes.tswith the global object of the route names inside.~/<outDir>/__useTypedRouter.tsComposable to simply access your typed routes~/<outDir>/typed-router.d.tscontaining the global typecript definitions and exports~/plugins/__typed_router.tsPlugin that will inject$typedRouterand$routesList(@nuxt/kithas problems registering plugin templates so this is a workaround)
You can specify the output dir of the generated files in your configuration. It defaults to <srcDir>/generated
import TypedRouter from 'nuxt-typed-router';
export default defineNuxtConfig({
buildModules: [TypedRouter],
nuxtTypedRouter: {
outDir: './generated',
},
});Given this structure
βββ pages
βββ index
βββ content
βββ [id].vue
βββ content.vue
βββ index.vue
βββ communication.vue
βββ statistics.vue
βββ [user].vue
βββ index.vue
βββ forgotpassword.vue
βββ reset-password.vue
β βββ login.vue
βββ ...
The generated route list will look like this
export const routerPagesNames = {
forgotpassword: 'forgotpassword' as const,
login: 'login' as const,
resetPassword: 'reset-password' as const,
index: {
index: 'index' as const,
communication: 'index-communication' as const,
content: {
id: 'index-content-id' as const,
},
statistics: 'index-statistics' as const,
user: 'index-user' as const,
},
};
export type TypedRouteList =
| 'forgotpassword'
| 'login'
| 'reset-password'
| 'index'
| 'index-communication'
| 'index-content-id'
| 'index-statistics'
| 'index-user';nuxt-typed-router will also create a plugin in your
<srcDir>/pluginsfolder with the injected$typedRouterand$routesListhelpers
useTypedRouter is an exported composable from nuxt-typed-router. It contains a clone of vue-router but with strictly typed route names and params type-check
<script lang="ts">
// The path here is `~/generated` because I set `outDir: './generated'` in my module options
import { useTypedRouter } from '~/generated';
export default defineComponent({
setup() {
// Fully typed
const { router, routes } = useTypedRouter();
function navigate() {
// Autocompletes the name and infer the params
router.push({ name: routes.index.user, params: { user: 1 } }); // β
valid
router.push({ name: routes.index.user, params: { foo: 1 } }); // β invalid
}
return { navigate };
},
});
</script>$typedRouter is an injected clone of vue-router $router, but fully typed with all your routes.
It's available anywhere you have access to Nuxt context
<script lang="ts">
import { defineComponent } from 'vue';
export default defineComponent({
name: 'Index',
setup() {
const { $typedRouter, $routesList } = useNuxtApp();
function navigate() {
$typedRouter.push({ name: $routesList.activate });
}
return {
navigate,
};
},
});
</script>You can import the useTypedRouter composable from where it's generated.
Exemple with pinia store here
import pinia from 'pinia';
import { useTypedRouter } from '~/generated';
export const useFooStore = defineStore('foo', {
actions: {
bar() {
const { router, routes } = useTypedRouter();
router.push({ name: routes.index.user, params: { user: 2 } });
},
},
});- Clone this repository
- Install dependencies using
yarn - Build project for local tests
yarn build:local - Start dev playground
yarn play - Build project for deploy
yarn prepack
