Skip to main content

$app/paths

import { function asset(file: Asset): string

Resolve the URL of an asset in your static directory, by prefixing it with config.kit.paths.assets if configured, or otherwise by prefixing it with the base path.

During server rendering, the base path is relative and depends on the page currently being rendered.

@examplesvelte <script> import { asset } from '$app/paths'; </script> <img alt="a potato" src={asset('/potato.jpg')} />
@since2.26
asset
, let assets: "" | `https://${string}` | `http://${string}` | "/_svelte_kit_assets"

An absolute path that matches config.kit.paths.assets.

If a value for config.kit.paths.assets is specified, it will be replaced with '/_svelte_kit_assets' during vite dev or vite preview, since the assets don’t yet live at their eventual URL.

@deprecatedUse asset(...) instead
assets
, let base: "" | `/${string}`

A string that matches config.kit.paths.base.

Example usage: <a href="{base}/your-page">Link</a>

@deprecatedUse resolve(...) instead
base
, function resolve<T extends RouteId | Pathname>(...args: ResolveArgs<T>): ResolvedPathname

Resolve a pathname by prefixing it with the base path, if any, or resolve a route ID by populating dynamic segments with parameters.

During server rendering, the base path is relative and depends on the page currently being rendered.

@examplejs import { resolve } from '$app/paths'; // using a pathname const resolved = resolve(`/blog/hello-world`); // using a route ID plus parameters const resolved = resolve('/blog/[slug]', { slug: 'hello-world' });
@since2.26
resolve
, function resolveRoute<T extends RouteId | Pathname>(...args: ResolveArgs<T>): ResolvedPathname
@deprecatedUse resolve(...) instead
resolveRoute
} from '$app/paths';

asset

自 2.26 起可用

¥Available since 2.26

解析 static 目录中资源的 URL,如果已配置,则通过在其前添加 config.kit.paths.assets 前缀,否则通过在其前添加基本路径前缀。

¥Resolve the URL of an asset in your static directory, by prefixing it with config.kit.paths.assets if configured, or otherwise by prefixing it with the base path.

在服务器渲染期间,基本路径是相对的,并且取决于当前正在渲染的页面。

¥During server rendering, the base path is relative and depends on the page currently being rendered.

<script>
	import { asset } from '$app/paths';
</script>

<img alt="a potato" src={asset('/potato.jpg')} />
function asset(file: Asset): string;

assets

改用 asset(...)

¥Use asset(...) instead

config.kit.paths.assets 匹配的绝对路径。

¥An absolute path that matches config.kit.paths.assets.

如果指定了 config.kit.paths.assets 的值,它将在 vite devvite preview 期间被替换为 '/_svelte_kit_assets',因为资源尚未存在于其最终 URL 中。

let assets:
	| ''
	| `https://${string}`
	| `http://${string}`
	| '/_svelte_kit_assets';

base

改用 resolve(...)

¥Use resolve(...) instead

config.kit.paths.base 匹配的字符串。

¥A string that matches config.kit.paths.base.

示例用法:<a href="{base}/your-page">Link</a>

¥Example usage: <a href="{base}/your-page">Link</a>

let base: '' | `/${string}`;

resolve

自 2.26 起可用

¥Available since 2.26

通过在路径名前添加基本路径(如果有)来解析路径名,或者通过在动态段中填充参数来解析路由 ID。

¥Resolve a pathname by prefixing it with the base path, if any, or resolve a route ID by populating dynamic segments with parameters.

在服务器渲染期间,基本路径是相对的,并且取决于当前正在渲染的页面。

¥During server rendering, the base path is relative and depends on the page currently being rendered.

import { function resolve<T extends RouteId | Pathname>(...args: ResolveArgs<T>): ResolvedPathname

Resolve a pathname by prefixing it with the base path, if any, or resolve a route ID by populating dynamic segments with parameters.

During server rendering, the base path is relative and depends on the page currently being rendered.

@examplejs import { resolve } from '$app/paths'; // using a pathname const resolved = resolve(`/blog/hello-world`); // using a route ID plus parameters const resolved = resolve('/blog/[slug]', { slug: 'hello-world' });
@since2.26
resolve
} from '$app/paths';
// using a pathname const const resolved: stringresolved = resolve<"/blog/hello-world">(route: "/blog/hello-world", params: Record<string, string>): ResolvedPathname

Resolve a pathname by prefixing it with the base path, if any, or resolve a route ID by populating dynamic segments with parameters.

During server rendering, the base path is relative and depends on the page currently being rendered.

@examplejs import { resolve } from '$app/paths'; // using a pathname const resolved = resolve(`/blog/hello-world`); // using a route ID plus parameters const resolved = resolve('/blog/[slug]', { slug: 'hello-world' });
@since2.26
resolve
(`/blog/hello-world`);
// using a route ID plus parameters const const resolved: stringresolved = resolve<"/blog/[slug]">(route: "/blog/[slug]", params: Record<string, string>): ResolvedPathname

Resolve a pathname by prefixing it with the base path, if any, or resolve a route ID by populating dynamic segments with parameters.

During server rendering, the base path is relative and depends on the page currently being rendered.

@examplejs import { resolve } from '$app/paths'; // using a pathname const resolved = resolve(`/blog/hello-world`); // using a route ID plus parameters const resolved = resolve('/blog/[slug]', { slug: 'hello-world' });
@since2.26
resolve
('/blog/[slug]', {
slug: stringslug: 'hello-world' });
function resolve<T extends RouteId | Pathname>(
	...args: ResolveArgs<T>
): ResolvedPathname;

resolveRoute

改用 resolve(...)

¥Use resolve(...) instead

function resolveRoute<T extends RouteId | Pathname>(
	...args: ResolveArgs<T>
): ResolvedPathname;
上一页 下一页