要创建具有动态参数的路由,请使用方括号括住有效的变量名。例如,像 src/routes/blog/[slug]/+page.svelte
这样的文件将创建与 /blog/one
、/blog/two
、/blog/three
等匹配的路由。
¥To create routes with dynamic parameters, use square brackets around a valid variable name. For example, a file like src/routes/blog/[slug]/+page.svelte
will create a route that matches /blog/one
, /blog/two
, /blog/three
and so on.
让我们创建该文件:
¥Let’s create that file:
<h1>blog post</h1>
我们现在可以从 /blog
页面导航到单个博客文章。在下一章中,我们将了解如何加载其内容。
¥We can now navigate from the /blog
page to individual blog posts. In the next chapter, we’ll see how to load their content.
多个路由参数可以出现在一个 URL 段中,只要它们之间至少有一个静态字符分隔即可:
foo/[bar]x[baz]
是有效路由,其中[bar]
和[baz]
是动态参数。¥[!NOTE] Multiple route parameters can appear within one URL segment, as long as they are separated by at least one static character:
foo/[bar]x[baz]
is a valid route where[bar]
and[baz]
are dynamic parameters.
<p>home</p>