Home > Web Front-end > uni-app > body text

How to dynamically configure routing information in uniapp

WBOY
Release: 2023-12-18 15:33:31
Original
1893 people have browsed it

How to dynamically configure routing information in uniapp

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'
    }
  },
  // ... 其他路由配置
]
Copy after login

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. metaAttributes 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()
Copy after login

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>
Copy after login

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.

Summary

The above are the detailed steps and sample code for dynamically configuring routing information in UniApp. Through this method, routing information can be dynamically configured according to specific business requirements, making the application more flexible and easier to maintain. Hope this article is helpful to everyone!

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template