Home >Web Front-end >uni-app >How do I handle routing and navigation in uni-app?
Uni-app utilizes a relatively straightforward routing system based on its own navigation API. Instead of relying on traditional browser-based routing like React Router or Vue Router, uni-app manages navigation internally. This means you primarily interact with the navigation using methods provided by the framework. The core method is uni.navigateTo()
, which pushes a new page onto the navigation stack. Other methods include uni.redirectTo()
, which replaces the current page, uni.reLaunch()
, which closes all pages and opens a new one, and uni.navigateBack()
, which pops a page from the stack. These methods are asynchronous and return a Promise, allowing you to handle success or failure. The pages themselves are defined in your pages.json
file, listing the path to each Vue component that represents a page. For example, to navigate to a page named 'detail' located at pages/detail/detail.vue
, you'd use uni.navigateTo({ url: '/pages/detail/detail' })
. The URL is relative to the pages
directory. Furthermore, uni-app supports tab bar navigation, allowing you to create applications with multiple bottom navigation tabs, each leading to a different set of pages. This is configured within pages.json
as well, specifying which pages belong to which tab.
Several best practices enhance the maintainability and user experience of your uni-app navigation:
uni.navigateTo()
for most cases: While other navigation methods exist, prioritize uni.navigateTo()
for most scenarios. This preserves the navigation history, allowing users to easily go back. Reserve uni.redirectTo()
and uni.reLaunch()
for specific situations where you want to completely replace the current page or clear the navigation stack.uni.setStorageSync()
: For simple data transfer, utilize URL parameters. However, for larger or sensitive data, leverage uni.setStorageSync()
to store data persistently across pages. Avoid passing large amounts of data directly via URL parameters, as it can impact performance and URL length..then()
and .catch()
to gracefully handle potential navigation failures. This makes your application more robust.Uni-app offers several ways to pass data between pages during navigation:
uni.navigateTo({ url: '/pages/detail/detail?id=123&name=John' })
. You can then access these parameters in the target page using uni.getCurrentPages()[uni.getCurrentPages().length - 1].options
.uni.navigateTo()
with data
option: For more complex data, you can pass an object via the data
option within uni.navigateTo()
. This data will be accessible in the target page's onLoad
lifecycle hook. For example: uni.navigateTo({ url: '/pages/detail/detail', data: { user: { id: 123, name: 'John' } } })
. Access the data in the detail page using this.$page.data
.uni.setStorageSync()
: For persistent data that needs to be accessible across multiple pages or even after navigation, utilize uni.setStorageSync()
to store data in the app's local storage. Retrieve it using uni.getStorageSync()
. This method is suitable for larger datasets or data that needs to persist beyond a single navigation instance. Remember to clear the storage when the data is no longer needed.pages.json
and ensure they accurately reflect the file structure of your pages. Typos or inconsistencies can lead to navigation errors.uni.reLaunch()
: While useful for specific scenarios, overusing uni.reLaunch()
can negatively impact the user experience by disrupting the navigation history and making it difficult to navigate back..then()
and .catch()
.uni.navigateTo()
's data
option or uni.setStorageSync()
.uni.setStorageSync()
, remember to clear the storage when the data is no longer needed. Leaving unnecessary data in storage can consume unnecessary space and potentially lead to unexpected behavior.The above is the detailed content of How do I handle routing and navigation in uni-app?. For more information, please follow other related articles on the PHP Chinese website!