您好,登錄后才能下訂單哦!
在Svelte 應用中實現(xiàn)動態(tài)路由和嵌套路由可以使用 Svelte Router 庫。以下是實現(xiàn)步驟:
npm install --save svelte-router
創(chuàng)建路由文件:創(chuàng)建一個名為 src/routes
的文件夾,并在其中創(chuàng)建路由文件,例如 Home.svelte
, About.svelte
, Profile.svelte
等等。
在 App.svelte
中引入 Router 組件:
<script>
import { Router, Route } from 'svelte-router';
import Home from './routes/Home.svelte';
import About from './routes/About.svelte';
import Profile from './routes/Profile.svelte';
</script>
<Router>
<Route path="/" component={Home} />
<Route path="/about" component={About} />
<Route path="/profile/:id" component={Profile} />
</Router>
<!-- Profile.svelte -->
<script>
export let id;
</script>
<h1>Profile Page</h1>
<p>User ID: {id}</p>
<!-- App.svelte -->
<Router>
<Route path="/" component={Home} />
<Route path="/about" component={About} />
<Route path="/profile/:id" component={Profile}>
<Route path="posts" component={Posts}/>
<Route path="comments" component={Comments}/>
</Route>
</Router>
這樣就可以在 Svelte 應用中實現(xiàn)動態(tài)路由和嵌套路由了。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。