Home > Article > Web Front-end > How to add global parameters in front of routes in vue
In Vue, we usually use routing to switch and jump between pages. In some special cases, we may need to add some global parameters to all routes to facilitate data transfer or control between various pages. This article will introduce how to add global parameters in front of Vue's routes.
1. Why do we need to add global parameters in front of routing
In Vue applications, we usually use routing to jump and switch between pages. Routing can control the display and behavior of the page by passing parameters. But in some cases, we may need to add some global parameters to all routes to facilitate the transfer of data and status between different pages, or to control the behavior of the page, such as dark mode, etc. At this time, we need to add global parameters in front of the route.
2. Method of adding global parameters in front of Vue's routing
In Vue's routing, we can add global parameters in front of the route by defining a global pre-guard on the router object. parameter. The global prepend guard is a function that will be executed before routing is switched. We can get the routing parameters in this function, add global parameters to them, and then return the new routing parameters.
The specific implementation steps are as follows:
1. In the index.js file under the router directory of the Vue project, define the global front guard.
// router/index.js import Vue from 'vue' import Router from 'vue-router' Vue.use(Router) const router = new Router({ routes: [ //路由配置 ] }) router.beforeEach((to, from, next) => { //定义全局参数 const globalParams = { title: 'Vue全局参数', mode: 'dark' }; //合并路由参数和全局参数 const params = Object.assign({}, to.params, globalParams); next({ path: to.path, params }); }); export default router;
In the above code, we define a global front guard for the router. This function will be executed before each route switch. In this function, we define global parameters and then merge them with routing parameters as new routing parameters for page switching.
2. Access global parameters in each component
In the above code, we have defined the global parameters and merged them with the routing parameters before performing routing switching. So how do you access these global parameters in each component? It's actually very simple, we just need to define these parameters in the component's props or data. For example:
// PageA.vue <template> <div> <h1>{{title}}</h1> <p>{{mode}}</p> </div> </template> <script> export default { props: ['title', 'mode'], //在props中定义全局参数 //组件逻辑代码 } </script>
In the above example, we defined the global parameters title and mode into the props of the component, so that these parameters can be accessed through this.title and this.mode when the page is rendered.
3. Summary
In Vue, adding global parameters to all routes can facilitate our cross-page data and status transfer and control. We can define global pre-guards, add global parameters before routing switching, and then access these parameters through props or data in each component. This technique allows us to better control the status and behavior of Vue applications, improving development efficiency and user experience.
The above is the detailed content of How to add global parameters in front of routes in vue. For more information, please follow other related articles on the PHP Chinese website!