导航路由器

Framework7 Svelte 与 Framework7 本身一样,都带有强大且灵活的路由器。为了使其工作,我们必须指定路由.

Framework7 Svelte 中唯一的区别在于,在 Svelte 中我们已经使用 Svelte 组件组合我们的应用程序,因此我们需要将我们的页面(Svelte 组件)映射到路由。这可以通过在路由的component属性中传递 Svelte 组件来完成。这里有一个基本示例:

<!-- App.svelte -->
<App { ...f7params }>
  <!-- Current View/Router, initial page will be loaded from home.svelte component -->
  <View main url="/" />
</App>
<script>
  // Import pages components
  import HomePage from 'home.svelte';
  import AboutPage from 'about.svelte';
  import LoginPage from 'login.svelte';

  /*
    Now we need to map components to routes.
    We need to pass them along with the F7 app parameters to <App> component
  */

  const f7params = {
    name: 'My App',
    // specify routes for app
    routes: [
      {
        path: '/',
        component: HomePage,
      },
      {
        path: '/about/',
        component: AboutPage,
      },
      {
        path: '/login/',
        component: LoginPage,
      },
    ],
  };
</script>
<!-- home.svelte -->
<Page name="home">
  <Navbar title="Home Page" />
  ...
  <Link href="/about/">About Page</Link>
  <Link href="/login/">Login Page</Link>
</Page>
<script>
  import { Page, Navbar, Link } from 'framework7-svelte';
</script>
<!-- about.svelte -->
<Page name="about">
  <Navbar title="About" />
  <!-- Page content -->
  ...
</Page>
<script>
  import { Page, Navbar } from 'framework7-svelte';
</script>
<!-- login.svelte -->
<Page name="login">
  <Navbar title="Login" />
  <!-- Page content -->
  ...
</Page>
<script>
  import { Page, Navbar } from 'framework7-svelte';
</script>

查看完整的路由文档来了解所有可能的路由选项,如何实现嵌套路由, 可路由选项卡可路由模态框.

向组件传递属性

可以将属性传递给路由加载的 Svelte 组件。有几种方法可以做到这一点。

首先,所有路由参数将自动作为属性传递给组件,例如

// route with params
{
  path: '/blog/:postId/comments/:commentId/',
  component: BlogPost,
}

所以如果我们通过/blog/45/comments/122/URL 导航,那么以下数据将传递给属性:

{
  postId: '45',
  commentId: '122',
}

另一个选项是在路由的options:

{
  path: '/some-page/',
  component: SomeComponent,
  options: {
    props: {
      foo: 'bar',
      bar: true,
    },
  },
}

中指定属性

f7router.navigate('/some-page/', {
  props: {
    foo: 'bar',
    bar: true,
  }
})

异步懒加载组件

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

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

或者使用async路由如果我们需要更多的控制:

{
  path: '/about/',
  async({ resolve }) {
    // dynamic import component, returns promise
    import('./pages/about.svelte').then((module) => {
      // resolve with component
      resolve({ component: module.default })
    });
  } ,
},

路由器 API

要访问路由实例并使用路由器 API你可以使用特殊的f7router组件属性:

<Page>
  ...
  <Link onClick={() => f7router.navigate('/about/')}>About</Link>
  <Link onClick={() => f7router.back()}>Go Back</Link>
</Page>
<script>
  import { onMount } from 'svelte';
  import { Page, Link } from 'framework7-svelte';

  // Router component will receive f7router prop with current Router instance
  export let f7router;
  // Router component will receive f7route prop with current route data
  export let f7route;

  onMount(() => {
    // output current route data
    console.log(f7route); // -> { url: '/foo/', query: { id: 3 }, ... }
  });
</script>
无噪 Logo
无噪文档
中文文档 · 复刻官网
查看所有 ↗