简体   繁体   English

如何使用 Svelte Routify 创建路由链接

[英]How to Create Route Links with Svelte Routify

I am learning how to use Routify to wire up some routes and links.我正在学习如何使用 Routify 连接一些路由和链接。 I need help.我需要帮助。

At the Routify docs it says:在 Routify 文档中它说:

"Links can be handled in an array of different ways. This might be the easiest way". “可以通过多种不同方式处理链接。这可能是最简单的方式”。

via: https://routify.dev/guide/navigation通过: https://routify.dev/guide/navigation

It then gives this example:然后给出了这个例子:

<script>
  import {isActive, url} from '@sveltech/routify'

  const links =
  [
    ['./index', 'Home'], //add index if you don't want siblings to be considered children
    ['./blog', 'Blog'],
    ['./about', 'About'],
    ['./contact', 'Contact']
  ]
</script>

<style>
  .active {font-weight: bold}
</style>

<ul>
  {#each links as [path, name]}
    <!-- Svelte magic. If isActive is true, the "active" class is applied. -->
    <li class:active={$isActive(path)}>
      <a href={$url(path)}>
        {name}
      </a>
    </li>
  {/each}
</ul>

Unfortunately it doesn't give any instructions on how to wire this up to your app (if it does it's too complex for me to figure out).不幸的是,它没有提供任何关于如何将其连接到您的应用程序的说明(如果这样做的话,我无法弄清楚它太复杂了)。

I am using the Routify Template and I have copied the above code into the following directory:我正在使用 Routify 模板,并且已将上述代码复制到以下目录中:

pages/_components/Navigation.svelte pages/_components/Navigation.svelte

I then attempted to import it into pages/index.svelte as so:然后我尝试将其导入 pages/index.svelte,如下所示:

pages/index.svelte页面/index.svelte

 <script>

  import Navigation from './_components/Navigation.svelte';

</script>

<div>
  <Navigation/>


</div>

The links appear at the top of the page.链接显示在页面顶部。 When I click them the endpoint follows this pattern:当我单击它们时,端点遵循以下模式:

http://localhost:5000/index/home

http://localhost:5000/index/blog

http://localhost:5000/index/about

http://localhost:5000/index/contact

In my /pages directory I have pages that match the pages listed in the code such as:在我的/pages目录中,我的页面与代码中列出的页面相匹配,例如:

/pages/Home.svelte /pages/Home.svelte

When I click the links my browser stays on the same page and does not embed the page associated with the link.当我单击链接时,我的浏览器停留在同一页面上,并且不嵌入与链接关联的页面。 Also, my browser developer tools say:另外,我的浏览器开发工具说:

Uncaught Error: Route could not be found for "/index/home".
    at urlToRoute (urlToRoute.js:15)
    at updatePage (navigator.js:13)
    at pushstate (navigator.js:60)
    at History.history.<computed> [as pushState] (navigator.js:51)

What do I need to do so that I can see the respective pages and why is their an intermediate directory named "index"?我需要做什么才能看到各个页面,为什么它们的中间目录名为“index”? How do I get rid of the word "index" in the url?如何摆脱 url 中的“索引”一词?


UPDATE更新

Okay I have the navigation listed on all pages and semi-working.好的,我在所有页面上都列出了导航并且处于半工作状态。

I have my Navigation code (below) imported into pages/_layout.svelte .我将导航代码(如下)导入pages/_layout.svelte

The code is as follows:代码如下:

pages/_layout.svelte页面/_layout.svelte

<script>
    import Navigation from './_components/Navigation.svelte';
</script>

<!--TABS-->

<Navigation/>

pages/_components/Navigation.svelte pages/_components/Navigation.svelte


<script>
  import {isActive, url} from '@sveltech/routify'

  const links =
  [
    ['./index', 'Home'], //add index if you don't want siblings to be considered children
    ['./form', 'Form']
  ]
</script>

<style>
  .active {font-weight: bold}
</style>

<ul>
  {#each links as [path, name]}
    <!-- Svelte magic. If isActive is true, the "active" class is applied. -->
    <li class:active={$isActive(path)}>
      <a href={$url(path)}>
        {name}
      </a>
    </li>
  {/each}
</ul>

The navigation links appear but the problem now is that the content of the selected page does not appear.出现导航链接,但现在的问题是所选页面的内容没有出现。

In the image below, the form endpoint is being rendered.在下图中,正在呈现表单端点。 The form component has working HTML but the HTML does not appear on the page.表单组件有工作 HTML 但 HTML 没有出现在页面上。

I am also getting a warning my my developer tools that says: " received an unexpected slot "default"我的开发人员工具也收到一条警告,上面写着:“收到了一个意外的插槽”“默认”

在此处输入图像描述

You shouldn't import your Navigation component into the index.svelte file but instead into a _layout.svelte file in the same directory.您不应将 Navigation 组件导入index.svelte文件,而应将其导入同一目录中的_layout.svelte文件。

They show the setup in the TV Shows App and although they don't show the above code imported in a Nav file like you're trying it will work if the component with the above code is imported in the _layout file since the url helper resolves paths relative to the page/layout in which it's used.他们在电视节目应用程序中显示了设置,尽管他们没有像您尝试的那样显示在导航文件中导入的上述代码,但如果将具有上述代码的组件导入到 _layout 文件中,它将起作用,因为 url 帮助程序解决了相对于使用它的页面/布局的路径。

Importing it into the index file creates paths relative to the index file which is why you're seeing index in all routes.将其导入索引文件会创建相对于索引文件的路径,这就是您在所有路由中看到索引的原因。

UPDATE:更新:

In response to needing to render the content of each page.响应需要渲染每个页面的内容。 This is done using a slot in the _layout file, in your example it would look like this:这是使用 _layout 文件中的一个插槽完成的,在您的示例中,它看起来像这样:

<Navigation/>

<main>
  <slot />
</main>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM