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 应用中扩展良好,因为主要的标签页通常始终可见。
🌐 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.tsxindex.tsxsingle page tabfeed_layout.tsxtab with a stack insideindex.tsx[postId].tsxsettings.tsxsingle page tab在 app/(tabs)/_layout.tsx 文件中,返回一个 Tabs 组件:
🌐 In the app/(tabs)/_layout.tsx file, return a Tabs component:
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:
import { Stack } from 'expo-router'; export const unstable_settings = { initialRouteName: 'index', }; export default function FeedLayout() { return <Stack />; }
现在,在 app/(tabs)/feed 目录下,你可以有指向不同帖子(例如,/feed/123)的 Link 组件。这些链接会将 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。将 withAnchor 与 initialRouteName 结合使用,以确保 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:
<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
路由组可以用来在两个不同的标签之间共享一个屏幕。考虑一个导航树,它有一个“动态”标签和一个“搜索”标签,并且它们都共享用于查看用户资料的页面:
🌐 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 tabsusers[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.
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:
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 将会在“信息流”标签页中显示该用户的个人资料。
🌐 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.tsxindex.tsxProtected settings.tsxProtected sign-in.tsxcreate-account.tsxmodal.tsxProtected 当你的应用首次启动时,路由将尝试打开根索引 app/(tabs)/index.tsx。如果你用 Stack.Protected 和 guard={false} 封装此屏幕,该屏幕将无法访问,并且会打开下一个可用屏幕。在此示例中,将打开 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.
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> ); }
这样,你可以从存储中获取你的身份验证状态并显示相应的界面。如果身份验证状态发生变化,布局会重新渲染,所以如果 isLoggedIn 从 false 变为 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:
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> ); }
请参考详细指南,了解如何使用受保护路由实现身份验证。
有时最佳路由根本不是路由
🌐 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:
import { Modal } from 'react-native'; import { SafeAreaView } from 'react-native-safe-area-context'; 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 中显示模态窗口的多种模式,包括在布局文件中使用模态窗口。