A router is a class component that extends RouterComponent
. Paste the following code inside the src/app/app.router.ts
file.
typescriptimport { RouterComponent } from "rasengan"; class AppRouter extends RouterComponent {}
CORE CONCEPTS
In this guide, you will learn how to configure routers in Rasengan.js, including setting up pages and layouts to structure your application effectively.
A Router in Rasengan.js is a collection of pages grouped under a common structure ( layout
). It defines how different pages are connected and whether they share a common Layout.
Each router:
To define a router, follow these steps:
A router is a class component that extends RouterComponent
. Paste the following code inside the src/app/app.router.ts
file.
typescriptimport { RouterComponent } from "rasengan"; class AppRouter extends RouterComponent {}
Use the defineRouter
function to configure the router by specifying:
typescriptimport { RouterComponent, defineRouter } from "rasengan"; class AppRouter extends RouterComponent {} export default defineRouter({ imports: [], // Import other routers layout: null, // Define a layout if needed pages: [] // Add pages })(AppRouter);
tsximport React from "react"; import { LayoutComponent, Outlet } from "rasengan"; const AppLayout: LayoutComponent = () => { return ( <div> <header>My Header</header> <Outlet /> <footer>My Footer</footer> </div> ); }; AppLayout.path = "/"; export default AppLayout;
tsximport { PageComponent } from "rasengan"; const Home: PageComponent = () => { return <div>Welcome to the Home Page</div>; }; Home.path = "/"; Home.metadata = { title: "Home", description: "Home Page" }; export default Home;
Once you have set up your layout and pages, update your app.router.ts
or app.router.js
file:
typescriptimport { RouterComponent, defineRouter } from "rasengan"; import AppLayout from "./app.layout"; import Home from "./home.page"; class AppRouter extends RouterComponent {} export default defineRouter({ imports: [], layout: AppLayout, pages: [Home] })(AppRouter);
sub routers
are routers that are nested within another router. They are useful for creating modular and complex applications routing structure.
Let's take a situation where you want to create an application with a dashboard and an authentication section. You can create a DashboardRouter
and an AuthRouter
and nest them within the AppRouter
.
To create a sub router, you can just define a new router and import it into the parent router.
typescriptimport { RouterComponent, defineRouter } from "rasengan"; class DashboardRouter extends RouterComponent {} export default defineRouter({ imports: [], layout: null, pages: [] })(DashboardRouter);
typescriptimport { RouterComponent, defineRouter } from "rasengan"; class AuthRouter extends RouterComponent {} export default defineRouter({ import: [], layout: null, pages: [] })(AuthRouter);
To nest the sub routers within the parent router, import the sub routers into the parent router and add them to the import
property.
typescriptimport { RouterComponent, defineRouter } from "rasengan"; import AppLayout from "./app.layout"; import Home from "./home.page"; import DashboardRouter from "./dashboard.router"; import AuthRouter from "./auth.router"; class AppRouter extends RouterComponent {} export default defineRouter({ imports: [DashboardRouter, AuthRouter], layout: AppLayout, pages: [Home] })(AppRouter);
Now you have two sub routers nested within the AppRouter
. These sub routers can have their own pages and layouts, making your application structure more modular and organized.
By default, sub routers
inherit the layout of the parent router. However, you can specify whether or not a sub router should use the parent router's layout by setting the useParentLayout
property to true
or false
.
typescriptimport { RouterComponent, defineRouter } from "rasengan"; class DashboardRouter extends RouterComponent {} export default defineRouter({ imports: [], layout: null, pages: [], useParentLayout: false // Do not use the parent layout })(DashboardRouter);