Navigation and routing (navigateTo, tabBar, etc.)
Basic Methods of Route Navigation
uni-app provides multiple route navigation methods, the most basic of which is the uni.navigateTo
method. This method retains the current page and navigates to a specified page within the app. You can return to the original page using uni.navigateBack
.
// Navigate to a specified page
uni.navigateTo({
url: '/pages/detail/detail?id=1'
});
// Receiving parameters on the target page
onLoad(options) {
console.log(options.id); // Outputs 1
}
Parameters can be passed during route navigation and are automatically appended to the URL. The target page retrieves these values through the options
parameter in the onLoad
lifecycle.
Special Route Navigation Methods
In addition to regular navigation, uni-app provides several special route methods:
uni.redirectTo
- Closes the current page and navigates to a specified page within the app.
uni.redirectTo({
url: '/pages/login/login'
});
uni.reLaunch
- Closes all pages and opens a specified page within the app.
uni.reLaunch({
url: '/pages/index/index'
});
uni.switchTab
- Navigates to a tabBar page and closes all other non-tabBar pages.
uni.switchTab({
url: '/pages/home/home'
});
tabBar Configuration and Usage
tabBar is a feature in uni-app for implementing a bottom navigation bar. It needs to be configured in pages.json
:
{
"tabBar": {
"color": "#999",
"selectedColor": "#007AFF",
"backgroundColor": "#ffffff",
"borderStyle": "black",
"list": [
{
"pagePath": "pages/home/home",
"text": "Home",
"iconPath": "static/tab-home.png",
"selectedIconPath": "static/tab-home-active.png"
},
{
"pagePath": "pages/user/user",
"text": "Profile",
"iconPath": "static/tab-user.png",
"selectedIconPath": "static/tab-user-active.png"
}
]
}
}
tabBar pages have some special restrictions:
- You cannot use
navigateTo
to navigate to a tabBar page; you must useswitchTab
. - tabBar pages do not reload when switched and retain their previous state.
Multiple Ways to Pass Route Parameters
In addition to passing simple parameters via the URL, you can also use the following methods to pass complex data:
- Global Variable Method
// Sending page
uni.navigateTo({
url: '/pages/detail/detail',
success: function(res) {
res.eventChannel.emit('acceptData', {data: 'Complex data'});
}
});
// Receiving page
onLoad(options) {
const eventChannel = this.getOpenerEventChannel();
eventChannel.on('acceptData', function(data) {
console.log(data); // {data: 'Complex data'}
});
}
- Vuex State Management
// store.js
state: {
routeParams: {}
},
mutations: {
setRouteParams(state, params) {
state.routeParams = params;
}
}
// Sending page
this.$store.commit('setRouteParams', {id: 123});
uni.navigateTo({url: '/pages/detail/detail'});
// Receiving page
computed: {
params() {
return this.$store.state.routeParams;
}
}
Route Interception and Permission Control
In real-world projects, it is often necessary to intercept route navigation to implement permission control features like login verification. This can be achieved by overriding route methods:
// Encapsulate route navigation method
const myNavigateTo = (options) => {
if (options.url.indexOf('/pages/member/') !== -1) {
if (!uni.getStorageSync('token')) {
uni.showToast({title: 'Please log in first', icon: 'none'});
uni.navigateTo({url: '/pages/login/login'});
return;
}
}
uni.navigateTo(options);
};
// Use the encapsulated method
myNavigateTo({
url: '/pages/member/center'
});
Route Animations and Custom Effects
uni-app supports configuring page transition animations:
uni.navigateTo({
url: '/pages/detail/detail',
animationType: 'slide-in-right',
animationDuration: 300
});
Supported animation types include:
slide-in-right
- Slides in from the right horizontallyslide-in-left
- Slides in from the left horizontallyslide-in-top
- Slides in from the top verticallyslide-in-bottom
- Slides in from the bottom verticallyfade-in
- Fades inzoom-out
- Zooms inzoom-fade-out
- Zooms and fades inpop-in
- Translates in from the right
Advanced Usage of Route APIs
- Get Current Route Stack Information
const pages = getCurrentPages();
const currentPage = pages[pages.length - 1];
console.log(currentPage.route); // Current page path
- Dynamically Modify Page Title
uni.setNavigationBarTitle({
title: 'New Title'
});
- Listen for Route Changes
// In App.vue
onShow() {
const pages = getCurrentPages();
const currentPage = pages[pages.length - 1];
this.$bus.$emit('routeChange', currentPage.route);
}
Common Issues and Solutions
- Page Refresh on Return
// Add listener on the previous page
onShow() {
if (this.$refs.list) {
this.$refs.list.refresh();
}
}
- Page Stack Exceeds 10-Layer Limit
// Use redirectTo instead of navigateTo
uni.redirectTo({
url: '/pages/newpage/newpage'
});
- tabBar Red Dot Notification
uni.showTabBarRedDot({
index: 1 // tabBar index
});
- Dynamically Hide tabBar
uni.hideTabBar({
animation: true
});
Cross-Platform Route Differences
Different platforms may have varying route behaviors, requiring compatibility handling:
// Determine platform
const isH5 = process.env.VUE_APP_PLATFORM === 'h5';
// Platform-specific handling
if (isH5) {
// H5-specific route logic
window.addEventListener('popstate', this.handlePopState);
} else {
// Mini-program/App route logic
uni.onAppRoute((res) => {
console.log(res.path);
});
}
Route Performance Optimization Techniques
- Preload Pages
uni.preloadPage({
url: '/pages/detail/detail'
});
- Subpackage Loading
// pages.json
{
"subPackages": [
{
"root": "pages/sub",
"pages": [
{
"path": "moduleA",
"style": {}
}
]
}
]
}
- Route Lazy Loading
// Dynamically import components
components: {
'lazy-component': () => import('@/components/lazy-component.vue')
}
Relationship Between Routes and Page Lifecycles
Understanding the order of page lifecycle triggers during route navigation is important:
-
Navigating from Page A to Page B:
- Page A: onHide
- Page B: onLoad → onShow → onReady
-
Navigating back from Page B to Page A:
- Page B: onUnload
- Page A: onShow
-
Using redirectTo:
- Original page: onUnload
- New page: onLoad → onShow → onReady
Secure Handling of Route Parameters
Handling route parameters requires attention to security:
- Parameter Validation
onLoad(options) {
const id = parseInt(options.id);
if (isNaN(id) || id <= 0) {
uni.redirectTo({url: '/pages/error/error'});
return;
}
this.loadData(id);
}
- Parameter Encoding/Decoding
// Encode when sending
uni.navigateTo({
url: `/pages/detail/detail?title=${encodeURIComponent('Parameter with special characters &value=1')}`
});
// Decode when receiving
onLoad(options) {
const title = decodeURIComponent(options.title);
}
Advanced Patterns for Route and Page Communication
- Using EventBus for Cross-Page Communication
// Sending page
this.$bus.$emit('data-update', {newData: 'value'});
// Receiving page
created() {
this.$bus.$on('data-update', this.handleDataUpdate);
},
beforeDestroy() {
this.$bus.$off('data-update', this.handleDataUpdate);
}
- Using Vuex for State Sharing
// Sending page
this.$store.commit('updateData', payload);
// Receiving page
computed: {
sharedData() {
return this.$store.state.sharedData;
}
}
Implementation of Route Hooks
Although uni-app does not directly provide route hooks, they can be simulated in the following ways:
- Global Route Hooks
// main.js
const originalNavigateTo = uni.navigateTo;
uni.navigateTo = function(options) {
console.log('About to navigate to:', options.url);
originalNavigateTo.call(uni, options);
};
- Page-Level Route Hooks
// In the page
onShow() {
if (this.$options.routeHook && this.$options.routeHook.beforeEnter) {
this.$options.routeHook.beforeEnter.call(this);
}
}
Routes and Page Scroll Position
Handling page scroll positions is a common requirement:
- Save Scroll Position
onPageScroll(e) {
this.scrollTop = e.scrollTop;
},
onHide() {
uni.setStorageSync('pageScroll_' + this.$route.path, this.scrollTop);
}
- Restore Scroll Position
onShow() {
const savedPosition = uni.getStorageSync('pageScroll_' + this.$route.path);
if (savedPosition) {
uni.pageScrollTo({
scrollTop: savedPosition,
duration: 0
});
}
}
Dynamic Routes and Permission Control
Generate route tables dynamically based on user permissions:
// Filter routes based on permissions
function filterRoutes(routes, permissions) {
return routes.filter(route => {
if (route.meta && route.meta.permission) {
return permissions.includes(route.meta.permission);
}
return true;
});
}
// Dynamically modify pages.json
const dynamicRoutes = filterRoutes(allRoutes, userPermissions);
uni.setTabBarItems({
items: dynamicRoutes.filter(r => r.isTab).map(r => ({
pagePath: r.path,
text: r.title,
iconPath: r.icon,
selectedIconPath: r.selectedIcon
}))
});
本站部分内容来自互联网,一切版权均归源网站或源作者所有。
如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn