Home> Web Front-end> Vue.js> body text

How to use Vue for route management and navigation control

WBOY
Release: 2023-08-02 09:13:11
Original
729 people have browsed it

How to use Vue for routing management and navigation control

Introduction:
In modern front-end development, single-page application (SPA) has become a mainstream development model. In SPA, routing management and navigation control are very important. The Vue framework provides a plug-in, Vue Router, for front-end routing management and navigation control. This article will introduce how to use Vue Router for routing management and navigation control, as well as some common usage examples.

  1. Installing and configuring Vue Router
    First, we need to install Vue Router in the Vue project. Execute the following command in the command line:

    npm install vue-router
    Copy after login

    Then in the entry file main.js of the Vue project, introduce and configure the Vue Router:

    import Vue from 'vue' import VueRouter from 'vue-router' Vue.use(VueRouter) const routes = [ // 定义路由规则 ] const router = new VueRouter({ routes // 在路由中传入定义的路由规则 }) new Vue({ router // 在Vue实例中注入Vue Router }).$mount('#app')
    Copy after login
  2. Define and use routes
    In the above configuration, we have defined the routes array to store routing rules. In the array, each routing item can be defined in the form of objects, including information such as routing paths and components. The sample code is as follows:

    const routes = [ { path: '/', component: Home }, { path: '/about', component: About } ]
    Copy after login

    In the above code, we define two routing items, corresponding to the root paths '/' and '/about'. Among them, Home and About are the corresponding component names.

  3. Rendering Route
    Next, we need to actually render the route in the Vue component. This can be achieved through the tag, which will dynamically render the corresponding component based on the current routing path. The sample code is as follows:

    Copy after login

    In the above code, we use the tag to render the route and display the matched component content here.

  4. Navigation control
    Vue Router provides two components and , which are used to implement navigation links and view rendering respectively. We can use to create a navigation link. The sample code is as follows:

    Home About
    Copy after login

    In the above code, we use the tag to implement the navigation link and specify the link through the to attribute. path.

  5. Nested routing
    In actual development, we may encounter multiple levels of routing requirements for some pages. Vue Router provides the function of nested routing to achieve this requirement. The sample code is as follows:

    const routes = [ { path: '/', component: Home, children: [ { path: '', component: HomeSubpage1 }, { path: 'subpage2', component: HomeSubpage2 } ] } ]
    Copy after login

    In the above code, we define a parent route '/', its corresponding component is Home, and two child routes are defined in its children attribute.

  6. Route guard
    Route guard is a very important function in Vue Router. It can perform some operations before and after route jump, such as verifying whether the user is logged in, permission control, etc. Vue Router provides three levels of guards: global guards, route-exclusive guards and intra-component guards. The sample code is as follows:

    router.beforeEach((to, from, next) => { // 在跳转前执行的逻辑 next() }) router.afterEach(() => { // 在跳转后执行的逻辑 })
    Copy after login

    In the above code, we use the beforeEach and afterEach methods to define the global front guard and post guard respectively.

Summary:
This article introduces how to use Vue Router for routing management and navigation control, and provides some code examples. In actual development, reasonable use of Vue Router can improve front-end development efficiency and make the code structure clearer and maintainable. I hope this article can help readers understand the use of Vue Router.

The above is the detailed content of How to use Vue for route management and navigation control. 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