路由设置

了解如何在 Expo Router 中使用静态属性配置布局。


警告 注意: unstable_settings 目前无法与 异步路由 一起使用(仅限开发环境)。这就是该功能被标记为 不稳定 的原因。

initialRouteName

当深度链接到某个路由时,你可能希望为用户提供一个“返回”按钮。initialRouteName 设置堆栈的默认屏幕,并且应该与有效的文件名(不含扩展名)匹配。

🌐 When deep linking to a route, you may want to provide a user with a "back" button. The initialRouteName sets the default screen of the stack and should match a valid filename (without the extension).

app
_layout.tsx
index.tsx
other.tsx
app/_layout.tsx
import { Stack } from 'expo-router'; export const unstable_settings = { // Ensure any route can link back to `/` initialRouteName: 'index', }; export default function Layout() { return <Stack />; }

现在直接深度链接到 /other 或重新加载页面时,仍然会显示返回箭头。

🌐 Now deep linking directly to /other or reloading the page will continue to show the back arrow.

使用array syntax (foo,bar) 时,你可以在 unstable_settings 对象中指定一个组的名称,以定位特定的段。

🌐 When using array syntax (foo,bar) you can specify the name of a group in the unstable_settings object to target a particular segment.

other.tsx
export const unstable_settings = { // Used for `(foo)` initialRouteName: 'first', // Used for `(bar)` bar: { initialRouteName: 'second', }, };

initialRouteName 仅在深度链接到路由时使用。在应用导航过程中,你正在导航的路由将是初始路由。你可以通过在 <Link /> 组件上使用 initial 属性或者将该选项传递给命令式 API 来禁用此行为。

🌐 The initialRouteName is only used when deep-linking to a route. During app navigation, the route you are navigating to will be the initial route. You can disable this behavior using the initial prop on the <Link /> component or by passing the option to the imperative APIs.

// If this navigates to a new _layout, don't override the initial route <Link href="/route" initial={false} />; router.push('/route', { overrideInitialScreen: false });