路由设置

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


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

initialRouteName

当深层链接到路由时,你可能希望为用户提供 "back" 按钮。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.

使用 数组语法 (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 prop 或将选项传递给命令式 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 });