适用于 Svelte 4 的轻量级、可交互的客户端路由器
Light & reactive client-side router for Svelte 4
npm i svelte-micro
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.
| 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 objecttype Router = {
go: (delta?: number) => void
push: (url?: string | URL | null, state?: any) => void
replace: (url?: string | URL | null, state?: any) => void
}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 storetype OptionsStore = {
subscribe: import('svelte/store').Readable['subscribe']
set: (changedOptions: Partial) => void
}type Options = {
mode: 'window' | 'hash'
basePath: null | string
}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 storetype Path = stringtype PathStore = import('svelte/store').ReadableThe store which contains current path.
query storetype Query = stringtype QueryStore = import('svelte/store').ReadableThe store which contains current query.
hash storetype Hash = stringtype HashStore = import('svelte/store').ReadableThe store which contains current hash.
Route component
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
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 actiontype LinkHandle = import('svelte/action').ActionThe 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 attributea[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 eventgetPathSegments functionexport type GetPathSegments = (path: string) => string[]The getPathSegments function divides path into segments.
For example: getPathSegments('/about-us/story') => ['/about-us', '/story'].
path, query, hash usage
{text}
{#if $hash === '#modal'}
{/if}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.
{router.replace('/redirect-target')}
You have been redirected
{isUserAuthenticated ? "Log out" : "Log in"}
{#if isUserAuthenticated}
Welcome!
Log out
{/if}
暂无开放 Issues,或尚未同步最近议题。