@sgpinkus/my-vue-router
Version:
A very simple Vue router
56 lines (46 loc) • 1.1 kB
Markdown
Simple ~300LOC alternative to vue-router, with many fewer features.
- Simple flat routing table - no nested.
- No entry guards, no async, no name views, no ...
- Using path-to-regexp for param parsing.
- Works transparently with
**main.ts**
```
import { createRouter } from './my-vue-router'
import HomePage from '@/pages/Home.vue';
import ContactPage from './pages/Contact.vue';
import NotFoundPage from './pages/NotFound.vue';
import App from './App.vue';
const app = createApp(App);
const routes = [
{
path: '/',
name: 'home',
component: HomePage,
},
{
path: '/contact',
name: 'contact',
component: ContactPage,
},
{
path: '/*pathMatch',
name: 'not-found',
component: NotFoundPage,
routeProp: true,
},
];
app.use(createRouter(routes));
```
**xxx.vue**
```
...
// Template components:
<route-view></route-view>
<route-link :path='/home'>HOME</route-link>
<route-link :name='contact'>CONTACT</route-link>
...
// Programatic routing:
$router.dispatch({ name: 'contact' });
```