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

1.0.1 • Public • Published

Svelte Router

基于配置的 Svelte 路由

使用

<script lang="ts">
  import {currentRoute} from '@roadhog/svelte-router'
  import { router } from '../router';
  router.replace('/users');
  router.push('/users');
  router.push('/users/1');
  router.push({ path: '/users/:id', params: { id: 123 } });
  router.push({ path: '/users/:id', params: { id: 123 }, query: { action: 'push' } });
  router.back();
  router.forward();
  router.go(-1);
  $:console.log('currentRoute', $currentRoute)
</script>

router.ts

import { createHashHistory } from '@roadhog/svelte-router';
import Home from './pages/home.svelte';
import Users from './pages/users.svelte';
import User from './pages/user.svelte';

export const router = createHashHistory({
  routes: [
    {
      path: '/',
      name: 'Home',
      component: Home,
    },
    {
      path: '/users',
      name: 'Users',
      component: Users,
    },
    {
      path: '/users/:id',
      name: 'User',
      component: User,
    },
  ],
});

App.svelte

<script lang="ts">
  import { RouterProvider, hashChange } from '@roadhog/svelte-router';
  import { router } from './router';

  hashChange(({ currentRoute, action }) => {
    console.log(action, '当前路由->', currentRoute);
  });
</script>

<RouterProvider {router}>
  <div>这是404页面</div>
</RouterProvider>

Package Sidebar

Install

npm i @roadhog/svelte-router

Weekly Downloads

1

Version

1.0.1

License

none

Unpacked Size

7.61 kB

Total Files

10

Last publish

Collaborators

  • roadhog