# Router
## API
### Initial
```tsx | pure
const app = new Application({
router: {
type: 'browser' // type default value is `browser`
}
})
// or
const app = new Application({
router: {
type: 'memory',
initialEntries: ['/']
}
})
```
### add Route
#### basic
```tsx | pure
import { RouteObject } from 'react-router-dom'
const app = new Application()
const Hello = () => {
return
Hello
}
// first argument is `name` of route, second argument is `RouteObject`
app.router.add('root', {
path: '/',
element:
})
app.router.add('root', {
path: '/',
Component: Hello
})
```
#### Component is String
```tsx | pure
app.addComponents({
Hello
})
app.router.add('root', {
path: '/',
Component: 'Hello'
})
```
#### nested
```tsx | pure
import { Outlet } from 'react-router-dom'
const Layout = () => {
return
Home
about
}
const Home = () => {
return Home
}
const About = () => {
return About
}
app.router.add('root', {
element:
})
app.router.add('root.home', {
path: '/home',
element:
})
app.router.add('root.about', {
path: '/about',
element:
})
```
It will generate the following routes:
```tsx | pure
{
element: ,
children: [
{
path: '/home',
element:
},
{
path: '/about',
element:
}
]
}
```
### remove Route
```tsx | pure
// remove route by name
app.router.remove('root.home')
app.router.remove('hello')
```
#### Router in plugin
```tsx | pure
class MyPlugin extends Plugin {
async load() {
// add route
this.app.router.add('hello', {
path: '/hello',
element: hello
,
})
// remove route
this.app.router.remove('world');
}
}
```
## Example
```tsx
/**
* defaultShowCode: true
*/
import React from 'react';
import { Link, Outlet } from 'react-router-dom';
import { Application } from '@nocobase/client';
const Home = () => Home
;
const About = () => About
;
const Layout = () => {
return
}
const app = new Application({
router: {
type: 'memory',
initialEntries: ['/']
}
})
app.router.add('root', {
element:
})
app.router.add('root.home', {
path: '/',
element:
})
app.router.add('root.about', {
path: '/about',
element:
})
export default app.getRootComponent();
```