How to dynamically configure routing information in UniApp
In UniApp, the configuration of routing information is very important, it determines the jumps of the user interface and the between pages association. By default, routing information is statically configured, that is, it is determined when the project is started. But sometimes we need to dynamically configure routing information according to different business needs. This article will introduce in detail how to dynamically configure routing information in UniApp and provide code examples.
1. Create a dynamic routing configuration file
First, we need to create a file specifically for dynamic routing configuration, such as dynamicRoutes.js
. In this file, we will dynamically configure routing information based on specific business needs. The following is an example:
export default [ { path: '/home', name: 'home', component: () => import('@/pages/home/index.vue'), meta: { title: '首页', icon: 'home' } }, { path: '/about', name: 'about', component: () => import('@/pages/about/index.vue'), meta: { title: '关于', icon: 'info' } }, // ... 其他路由配置 ]
In the above example, we configured two routing information: /home
and /about
, corresponding to home respectively
and about
two pages. Among them, the component
attribute uses the import()
dynamic import method to introduce page components. meta
Attributes are used to configure some additional information, such as page titles and icons.
2. Dynamically register routing
Next, we need to register the dynamically configured routing information into the UniApp routing system when the project starts. We can operate in the main.js
file. The following is the sample code:
import Vue from 'vue' import App from './App' import dynamicRoutes from './dynamicRoutes' // 动态注册路由 dynamicRoutes.forEach(route => { router.addRoute(route) }) Vue.config.productionTip = false App.mpType = 'app' const app = new Vue({ ...App }) app.$mount()
In the above example, we first introduce the dynamically configured routing information, and then use the forEach
method to traverse each routing configuration item and pass the router .addRoute(route)
Dynamicly register in the routing system.
3. Use dynamically configured routing information
After dynamically configuring and registering routing information according to the above steps, we can use these dynamically configured routing information in the page. The following is an example:
<template> <view> <text>{{ route.meta.title }}</text> </view> </template> <script> export default { onLoad() { // 获取当前页面路由对象 const route = getCurrentPages()[getCurrentPages().length - 1].$route console.log(route.meta.title) } } </script>
In the above example, we obtain the routing object route
of the current page through the getCurrentPages()
method, and then use ## directly #route.meta.titleGet the title of the page.
The above is the detailed content of How to dynamically configure routing information in uniapp. For more information, please follow other related articles on the PHP Chinese website!