Navigation Router

    The only difference in Framework7 Svelte is that in Svelte we are already composing our application with Svelte components, so we need to map our Pages (Svelte components) to the routes. It can be done by passing Svelte component in property of the route. Here’s a basic example:

    1. <!-- home.svelte -->
    2. <Page name="home">
    3. <Navbar title="Home Page" />
    4. ...
    5. <Link href="/about/">About Page</Link>
    6. <Link href="/login/">Login Page</Link>
    7. </Page>
    8. <script>
    9. import { Page, Navbar, Link } from 'framework7-svelte';
    10. </script>
    1. <!-- about.svelte -->
    2. <Page name="about">
    3. <Navbar title="About" />
    4. <!-- Page content -->
    5. ...
    6. </Page>
    7. <script>
    8. import { Page, Navbar } from 'framework7-svelte';
    9. </script>

    Check the full to know about all possible routes options, how to use Nested Routes, and Routable Modals.

    It is possible to pass component props to Svelte components loaded by router. There are few ways to do it.

    First of all, all route params will be automatically passed as props to component, e.g.

    1. {
    2. path: '/blog/:postId/comments/:commentId/',
    3. component: BlogPost,
    4. }
    1. {
    2. postId: '45',
    3. commentId: '122',
    4. }

    Another option is to specify props in route’s options:

    And finally, props can be passed dynamically to route component when we navigate with API:

    1. f7router.navigate('/some-page/', {
    2. props: {
    3. foo: 'bar',
    4. bar: true,
    5. }
    6. })

    With Webpack it is possible to load page components on demand, it is possible with F7’s route asyncComponent, for example:

    1. {
    2. path: '/about/',
    3. asyncComponent: () => import('./pages/about.svelte'),
    4. },

    Or with async route if we need more control over it:

    1. <Page>
    2. ...
    3. <Link onClick={() => f7router.navigate('/about/')}>About</Link>
    4. <Link onClick={() => f7router.back()}>Go Back</Link>
    5. </Page>
    6. <script>
    7. import { onMount } from 'svelte';
    8. import { Page, Link } from 'framework7-svelte';
    9. // Router component will receive f7router prop with current Router instance
    10. export let f7router;
    11. // Router component will receive f7route prop with current route data
    12. export let f7route;
    13. onMount(() => {
    14. // output current route data
    15. console.log(f7route); // -> { url: '/foo/', query: { id: 3 }, ... }
    16. });
    17. </script>