百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
S

svelte-micro

> 前端框架
开源

适用于 Svelte 4 的轻量级、可交互的客户端路由器

99 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

适用于 Svelte 4 的轻量级、可交互的客户端路由器

Svelte Micro

Light & reactive client-side router for Svelte 4

Table of content

  • Installation
  • Example
  • API
    • Imports reference
    • router object
    • options store
    • path store
    • query store
    • hash store
    • Route component
    • Link component
    • linkHandle action
    • getPathSegments function
  • Tips
    • path, query, hash usage
    • Scroll behavior control
    • Redirect
    • Guarded route

Installation

bash
npm i svelte-micro

Example

svelte

  
  
    Home
    Portfolio
    Our story
    Github
  

  
  
    
Home page

    
Make yourself at home.

  

  
    
Portfolio

    
    
      
Portfolio main page

      Sites
      Apps
    

    
      
Sites

      Back to portfolio main page
    

    
      
Apps

      Back to portfolio main page
    

    
      
The route is not found in /portfolio

      Back to portfolio main page
    
  

  
    
Our story

  

  
    
The route is not found

    Back to home
  

This code shows the capabilities of the svelte-micro routing system.
Spend a minute analyzing this example to understand the approach.

For advanced examples see the Tips section.

API

Imports reference

Entity Related imports
router object import { router, type Router } from 'svelte-micro'
options store import { options, type OptionsStore, type Options } from 'svelte-micro'
path store import { path, type PathStore, type Path } from 'svelte-micro'
query store import { query, type QueryStore, type Query } from 'svelte-micro'
hash store import { hash, type HashStore, type Hash } from 'svelte-micro'
Route component import { Route } from 'svelte-micro'
Link component import { Link } from 'svelte-micro'
linkHandle action import { linkHandle, type LinkHandle } from 'svelte-micro'
getPathSegments function import { getPathSegments, type GetPathSegments } from 'svelte-micro'

router object

Type definition

typescript
type Router = {
  go: (delta?: number) => void
  push: (url?: string | URL | null, state?: any) => void
  replace: (url?: string | URL | null, state?: any) => void
}

Description

The router object is an object whose methods allow to manipulate history.

  • router.go
    Move on delta steps through the history.

  • router.push
    Push new url and state to the history.

  • router.replace
    Replace current url and state in the history.

options store

Type definition

typescript
type OptionsStore = {
  subscribe: import('svelte/store').Readable['subscribe']
  set: (changedOptions: Partial) => void
}
typescript
type Options = {
  mode: 'window' | 'hash'
  basePath: null | string
}

Description

The options store provides subscribe and set methods to access and modify router options.

  • $options.mode
    Default: 'window'
    Set the mode for the router.

  • $options.basePath
    Default: null
    Set the basePath for the router.
    If a basePath value is not found at the beginning of $path, the router will continue to operate properly, ignoring the basePath option for this state of $path. Be aware that if mode is set to 'hash', the router will try to find the basePath value in the hash location fragment, since the hash location fragment is already separated from the path location fragment.

path store

Type definition

typescript
type Path = string
typescript
type PathStore = import('svelte/store').Readable

Description

The store which contains current path.

query store

Type definition

typescript
type Query = string
typescript
type QueryStore = import('svelte/store').Readable

Description

The store which contains current query.

hash store

Type definition

typescript
type Hash = string
typescript
type HashStore = import('svelte/store').Readable

Description

The store which contains current hash.

Route component

Type definition

svelte

  

Description

The Route component defines a route. The props of Route are reactive. A nested Route component works in context of its parental Route component, so you don't need to define its full path.

  • fallback
    Default: {false} The property which defines if the route is fallback. A fallback route is active when there is no active routes on its depth.

  • path
    Default: '/' The property which defines route path. path must start from '/'.

The top-level (root) Route must have path equal to '/' and fallback equal to false.
These values are set by default, so you can leave them unchanged (see Example section).

Link component

Type definition

svelte

  

Description

The `` component is built on top of linkHandle and should be used for the internal application navigation.
It automatically prevents the window from refreshing.

  • href
    Default: '/' The property which defines link href.

  • {...restProps}
    Any other property is attached on the inner a element.

If the basePath option isn't set to null, the component will append the `basePath` value to the `href` attribute.\ If the [`mode` option](#options-store) is set to `"hash"`, the component will append a # to the beginning of the href attribute.

linkHandle action

Type definition

typescript
type LinkHandle = import('svelte/action').Action

Description

The linkHandle action prevents window from refreshing when the click event occurs on a handled a[href] element.
linkHandle can be applied on a parental element to handle nested a[href] elements.

linkHandle ignores an a[href] element if:

  • a[href] has data-handle-ignore attribute
  • a[href] has target attribute which isn't equal to '_self'
  • a[href] has external href (new URL(href).origin !== document.location.origin)
  • (event.ctrlKey || event.metaKey || event.altKey || event.shiftKey) === true during the click event

getPathSegments function

Type definition

typescript
export type GetPathSegments = (path: string) => string[]

Description

The getPathSegments function divides path into segments.

For example: getPathSegments('/about-us/story') => ['/about-us', '/story'].

Tips

path, query, hash usage

svelte

{text}

{#if $hash === '#modal'}
  

{/if}

Scroll behavior control

javascript
import { path } from 'svelte-micro'

// Disable browser scroll behavior control
if ('scrollRestoration' in history) {
  history.scrollRestoration = 'manual'
}

// On path change reset scroll position
path.subscribe(() => window.scrollTo(0, 0))

By default svelte-micro doesn't control scroll behavior, but it's easy to do on your own.

Redirect

svelte

  
    {router.replace('/redirect-target')}
  

  
    
You have been redirected

  

Guarded route

svelte

  
    {isUserAuthenticated ? "Log out" : "Log in"}
  

  {#if isUserAuthenticated}
    
      
Welcome!

      Log out
    
  {/if}

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScriptlightreactiveroutersvelte

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类前端框架
定价开源

> 相关工具

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架