Expo Router 中常见的导航模式

将 Expo Router 基础知识应用于你可以在应用中使用的实际导航模式。


现在你已经了解了 Expo Router 中文件和目录的命名和排列方式,让我们运用这些知识,看看你可能在应用中使用的一些实际导航模式。

¥Now that you know the basics of how files and directories are named and arranged in Expo Router, let's apply that knowledge, looking at some real-life navigation patterns you might use in your app.

选项卡内的堆栈:嵌套导航器

¥Stacks inside tabs: nested navigators

如果你的应用通常以一组选项卡为起点,但一个或多个选项卡可能关联多个屏幕,则通常可以在选项卡内嵌套堆栈导航器。这种模式通常会生成直观的 URL,并且能够很好地扩展到桌面 Web 应用,因为桌面 Web 应用的主选项卡通常始终可见。

¥If the typical starting point for your app is a set of tabs, but one or more tabs may have more than one screen associated with it, nesting a stack navigator inside of a tab is often the way to go. This pattern often results in intuitive URLs and scales well to desktop web apps, where the primary tabs are often always visible.

考虑以下导航树:

¥Consider the following navigation tree:

app
(tabs)
  _layout.tsx
  index.tsxsingle page tab
  feed
   _layout.tsxtab with a stack inside
   index.tsx
   [postId].tsx
  settings.tsxsingle page tab

在 app/(tabs)/_layout.tsx 文件中,返回一个 Tabs 组件:

¥In the app/(tabs)/_layout.tsx file, return a Tabs component:

app/(tabs)/_layout.tsx
import { Tabs } from 'expo-router'; export default function TabLayout() { return ( <Tabs screenOptions={{ headerShown: false }}> <Tabs.Screen name="index" options={{ title: 'Home' }} /> <Tabs.Screen name="feed" options={{ title: 'Feed' }} /> <Tabs.Screen name="settings" options={{ title: 'Settings' }} /> </Tabs> ); }

在 app/(tabs)/feed/_layout.tsx 文件中,返回一个 Stack 组件:

¥In the app/(tabs)/feed/_layout.tsx file, return a Stack component:

app/(tabs)/feed/_layout.tsx
import { Stack } from 'expo-router'; export const unstable_settings = { initialRouteName: 'index', }; export default function FeedLayout() { return <Stack />; }

现在,在 app/(tabs)/feed 目录中,你可以拥有指向不同帖子的 Link 组件(例如 /feed/123)。这些链接会将 feed/[postId] 路由推送到堆栈,使标签导航器可见。

¥Now, within the app/(tabs)/feed directory, you can have Link components that point to different posts (for example, /feed/123). Those links will push the feed/[postId] route onto the stack, leaving the tab navigator visible.

你还可以使用相同的 URL 从任何其他选项卡导航到 feed 选项卡中的帖子。将 withAnchorinitialRouteName 结合使用,可确保 feed/index 路由始终是堆栈中的第一个屏幕:

¥You can also navigate from any other tab to a post in the feed tab with the same URL. Use withAnchor in conjunction with initialRouteName to ensure that the feed/index route is always the first screen in the stack:

app/(tabs)/feed/index.tsx
<Link href="/feed/123" withAnchor> Go to post </Link>

你还可以将选项卡嵌套在外部堆栈导航器中。这通常更适合在标签页上显示模态框。

¥You can also nest tabs inside of an outer stack navigator. That is often more useful for displaying modals over the tabs.

嵌套导航器

了解更多关于如何在 Expo Router 应用中使用嵌套导航器的信息。

一屏两屏标签页:共享路由

¥One screen, two tabs: sharing routes

路由组可用于在两个不同的选项卡之间共享单个屏幕。假设导航树包含一个“Feed”选项卡和一个“Search”选项卡,它们共享用于查看用户个人资料的页面:

¥Route groups can be used to share a single screen between two different tabs. Consider a navigation tree that has a Feed tab and a Search tab, and they both share pages for viewing a user profile:

app
(tabs)
  _layout.tsx
  (feed)
   index.tsxdefault route
  (search)
   search.tsx
  (feed,search)
   _layout.tsxlayout shared between the two tabs
   users
    [username].tsxshared user profile page

每个标签页都放在一个组中,因此你可以定义第三个目录,在两个组之间共享路由 (app/(tabs)/(feed,search)/)。即使有额外的层,app/(tabs)/(feed)/index.tsx 仍然是最近的索引,因此它将是默认路由。

¥Each of the tabs is put in a group so you can define a third directory that shares routes between two groups (app/(tabs)/(feed,search)/). Even with the extra layer, app/(tabs)/(feed)/index.tsx is still the nearest index, so it will be the default route.

app/(tabs)/_layout.tsx
import { Tabs } from 'expo-router'; export default function TabLayout() { return ( <Tabs> <Tabs.Screen name="(feed)" options={{ title: 'Feed' }} /> <Tabs.Screen name="(search)" options={{ title: 'Search' }} /> </Tabs> ); }

(feed)(search) 路由组都包含堆栈,因此它们也可以共享单个布局:

¥Both the (feed) and (search) route groups contain stacks, so they can also share a single layout:

app/(tabs)/(feed,search)/_layout.tsx
import { Stack } from 'expo-router'; export default function SharedLayout() { return <Stack />; }

共享组也可以仅包含共享页面,每个不同的组都有自己的布局文件。

¥It's also possible for shared groups to only contain the shared pages, with each distinct group having its own layout file.

现在,两个标签页都可以导航到 /users/evanbacon 并显示相同的用户资料页面。

¥Now, both tabs can navigate to /users/evanbacon and see the same user profile page.

当你已将焦点放在某个选项卡上并导航到某个用户时,你将停留在当前选项卡的组中。但是,当从应用外部直接深度链接到用户个人资料页面时,Expo Router 必须从两个组中选择一个,因此它将按字母顺序选择第一个组。因此,深度链接到 /users/evanbacon 将在“Feed”选项卡中显示用户配置文件。

¥When you're already focused on a tab and navigating to a user, you will stay in that current tab's group. But when deep-linking directly to a user profile page from outside the app, Expo Router has to pick one of the two groups, so it will pick the first group alphabetically. Therefore, deep-linking to /users/evanbacon will show the user profile in the Feed tab.

共享路由

了解更多关于如何在 Expo Router 中让不同的路由共享同一个 URL 的信息。

仅限已验证用户:受保护的路由

¥Authenticated users only: protected routes

对于需要身份验证的移动应用,你可能会有一组只能由经过身份验证的用户访问的路由。

¥For mobile apps requiring authentication, you will likely have a set of routes that should only be accessible to authenticated users.

例如,考虑以下导航树,其中有一个底部选项卡布局、一个登录页面、一个创建账户页面和一个仅对经过身份验证的用户可见的模态窗口:

¥For example, consider the following navigation tree in which you have a bottom tabs layout, a sign-in page, a create account page, and a modal that should only be visible to authenticated users:

app
_layout.tsxRoot layout
(tabs)
  _layout.tsx
  index.tsxProtected
  settings.tsxProtected
sign-in.tsx
create-account.tsx
modal.tsxProtected

当你的应用首次启动时,路由将尝试打开根索引 app/(tabs)/index.tsx。如果你将此屏幕与 guard={false} 一起封装在 Stack.Protected 中,则该屏幕将无法访问,而是会打开下一个可用屏幕。在本例中,sign-in 窗口将被打开,因为它是下一个可用路径。

¥When your app is first launched, the router will try to open the root index, app/(tabs)/index.tsx. If you wrap this screen in a Stack.Protected with the guard={false}, the screen will become inaccessible and the next available screen will be opened instead. In this example, the sign-in screen will be opened, since it is the next available route.

app/_layout.tsx
import { Stack } from 'expo-router'; import { useAuthState } from '@/utils/authState'; export default function RootLayout() { const { isLoggedIn } = useAuthState(); return ( <Stack> <Stack.Protected guard={isLoggedIn}> <Stack.Screen name="(tabs)" /> <Stack.Screen name="modal" /> </Stack.Protected> <Stack.Protected guard={!isLoggedIn}> <Stack.Screen name="sign-in" /> <Stack.Screen name="create-account" /> </Stack.Protected> </Stack> ); }

这样,你可以从存储中获取身份验证状态并显示相应的屏幕。如果授权状态发生变化,布局将重新渲染,因此如果 isLoggedInfalse 更改为 true,应用将自动导航到 (tabs) 组的根目录。

¥This way, you can fetch your auth state from a store and show the appropriate screens. If the auth state changes, the layout will re-render, so if isLoggedIn changes from false to true, the app will automatically navigate to the root of the (tabs) group.

受保护路由的另一个好处是,即使你直接深度链接到页面,它们也会被检查。例如,如果未经身份验证的用户深度链接到上面的模态窗口,他们将被重定向到登录页面。

¥Another benefit of protected routes is that they are checked even if you deep link into a page directly. For example, if an unauthenticated user deep links into the modal screen above, they will be redirected to the sign-in page.

受保护的路由也可用于有条件地显示底部标签页。在本例中,vip 选项卡仅向经过身份验证的 VIP 会员显示:

¥Protected routes can also be used to conditionally show bottom tabs. In this example, the vip tab will only be shown to authenticated users who are VIP members:

app/(tabs)/_layout.tsx
import { Stack } from 'expo-router'; import { useAuthState } from '@/utils/authState'; export default function TabsLayout() { const { isVip } = useAuthState(); return ( <Tabs> <Tabs.Screen name="index" /> <Tabs.Protected guard={isVip}> <Tabs.Screen name="vip" /> </Tabs.Protected> <Tabs.Screen name="settings" /> </Tabs> ); }
Expo Router 身份验证

遵循使用受保护路由实现身份验证的深入指南。

有时最佳路由根本不是路由

¥Sometimes the best route isn't a route at all

将导航状态划分为不同的路由是为了更好地服务你和你的应用。有时,最适合该任务的模式根本不涉及导航到其他路由。由于布局文件只是 React 组件,因此你可以使用它们来显示导航器周围、旁边或替代导航器的各种 UI。

¥Separating your navigation states into distinct routes is meant to serve you and your app. Sometimes the best pattern for the job will not involve navigating to another route at all. Since layout files are just React components, you can use them to display all sorts of UI around, besides, or instead of a navigator.

回想一下身份验证,如果用户无法在未登录的情况下访问某些页面,则受保护的路由设置非常有效。但是,当未经身份验证的用户以只读模式浏览应用时该怎么办?在这种情况下,你可能希望在应用上方显示登录模式,而不是将用户重定向到登录页面:

¥Thinking back to authentication, the protected route setup works great if the user should simply not be able to visit certain pages without logging in. But what about when unauthenticated users can browse an app in read-only mode? In that case, you might want to show a login modal over the app, rather than redirecting the user to a login page:

app/(logged-in)/_layout.tsx
import { SafeAreaView, Modal } from 'react-native'; import { Stack } from 'expo-router'; export default function Layout() { const isAuthenticated = /* check for valid auth token / session */ return ( <SafeAreaView> <Stack /> <Modal visible={!isAuthenticated}>{/* login UX */}</Modal> </SafeAreaView> ); }
Expo Router 中的模态框

了解在 Expo Router 中显示模态框的多种模式,包括在布局文件中使用模态框。