search
HomeWeb Front-endVue.jsHow to configure Vue3 routing, perform route jumps and pass parameters?

    1. Install the routing

    npm i vue-router

    2. Write the routing that needs to be displayed

    Create the pages folder in the src directory and create it inside The two vue files are named student.vue, person.vue

    How to configure Vue3 routing, perform route jumps and pass parameters?

    Write two vue files respectively

    student.vue and person.vue

    <template>
        学生
    </template>
     
    <script setup>
     
    </script>
     
    <style scoped lang="less">
     
    </style>
    <template>
    人类
    </template>
     
    <script setup>
     
    </script>
     
    <style scoped lang="less">
     
    </style>

    3. Configure routing

    Configure the router.js file in the src directory

    import { createRouter,createWebHistory } from "vue-router";
    const router=createRouter({
        history:createWebHistory(),
        routes:[
            {
                component:()=>import(&#39;../pages/person.vue&#39;),
                name:&#39;person&#39;,
                path:&#39;/person&#39;
            },
            {
                component:()=>import(&#39;../pages/student.vue&#39;),
                name:&#39;student&#39;,
                path:&#39;/student&#39;
            },
            {
                //实现路由重定向,当进入网页时,路由自动跳转到/student路由
                redirect:&#39;/student&#39;,
                path:&#39;/&#39;
            }
        ]
    })
    export default router

    3. Use routing

    Use routing in main.js

    import { createApp } from &#39;vue&#39;
    import App from &#39;./App.vue&#39;
    import router from &#39;./router&#39;
     
    createApp(App).use(router).mount(&#39;#app&#39;)

    Display routes in app.vue, use router-link to jump routes, to represents which route to jump to

    <template>
      <router-view></router-view>
      <hr>
      <div>
        <router-link to="/student">到student路由</router-link>
        <br>
        <router-link to="/person">到person路由</router-link>
      </div>
    </template>
     
    <script setup>
     
    </script>
    <style scoped>
     
    </style>

    The effect is as shown in the figure below, click (to student route) or ( to person routing) will perform route jump

    How to configure Vue3 routing, perform route jumps and pass parameters?

    4, programmatic routing

    Declarative routing performs route jump through router-link, programmatic routing Implemented through functions

    Modify app.vue, vue3 uses a combined API, you need to introduce

    useRouter, useRoute, and

    const router=useRouter()

    const route=useRoute()

    <template>
      <router-view></router-view>
      <hr>
      <div>
        <button @click="toStudent">到student路由</button>
        <br>
        <button @click="toPerson">到person路由</button>
      </div>
    </template>
     
    <script setup>
    import {useRouter,useRoute} from &#39;vue-router&#39;
    const router=useRouter()
    const route=useRoute()
    const toStudent=()=>{
      router.push(&#39;student&#39;)
    }
    const toPerson=()=>{
      router.push(&#39;person&#39;)
    }
    </script>
    <style scoped>
     
    </style>

    Route hop through router.push Transfer

    Use router router between routes, and use toute route for the current route

    The result is as shown in the figure below, realizing programmatic route jump

    How to configure Vue3 routing, perform route jumps and pass parameters?

    If no alias is set when configuring routing, you need to jump through the router.push configuration object

    const toStudent=()=>{
      router.push({
        path:&#39;/student&#39;
      })
    }
    const toPerson=()=>{
      router.push({
        path:&#39;/person&#39;
      })
    }

    5. Routing parameters

    5. 1Query parameter transfer

    Pass the id and name to the student route

    const toStudent=()=>{
      router.push({
        path:&#39;/student&#39;,
        query:{
          id:1,
          name:&#39;张三&#39;
        }
      })
    }

    The student route receives the query parameter

    <template>
        学生组件
        <div>{{data.query}}</div>
    </template>
     
    <script setup>
    import { reactive } from &#39;vue&#39;;
    import {useRouter,useRoute} from &#39;vue-router&#39;
    const route=useRoute()
    let data=reactive({
        query: route.query
    })
    </script>

    The effect is as shown in the figure below

    How to configure Vue3 routing, perform route jumps and pass parameters?

    5, 2 Pass params parameters

    Assuming that params parameters are passed to person routing, they need to be modified during routing configuration

    You need to use name to pass params parameters Specify the route

    const toPerson=()=>{
      router.push({
        name:&#39;person&#39;,
        params:{
          keyword:2
        }
      })
    }

    At the same time, the routing configuration needs to be modified. Assuming that the keyword is passed,

    needs to use placeholders and keywords in the path

    ? Indicates that it can be passed or not

    {
          component:()=>import(&#39;../pages/person.vue&#39;),
          name:&#39;person&#39;,
          path:&#39;/person/:keyword?&#39;
    },

    Receive params parameter in person.vue

    <template>
        人类组件
        <div>{{data.params.keyword}}</div>
    </template>
     
    <script setup>
    import { reactive } from &#39;vue&#39;;
    import {useRouter,useRoute} from &#39;vue-router&#39;
    const route=useRoute()
    let data=reactive({
        params: route.params
    })
    </script>

    The effect is as follows

    How to configure Vue3 routing, perform route jumps and pass parameters?

    6 , Sub-routing configuration

    Add sub-components (stu1, stu2 components) to the student route

    How to configure Vue3 routing, perform route jumps and pass parameters?

    The path of the sub-component does not contain /

    {
                component:()=>import(&#39;../pages/student.vue&#39;),
                name:&#39;student&#39;,
                path:&#39;/student&#39;,
                children:[
                    {
                        path:&#39;stu1&#39;,
                        name:&#39;stu1&#39;,
                        component:()=>import(&#39;../pages/stu1.vue&#39;)
                    },
                    {
                        path:&#39;stu2&#39;,
                        name:&#39;stu2&#39;,
                        component:()=>import(&#39;../pages/stu2.vue&#39;)
                    },
                    {
                        path:&#39;&#39;,
                        component:()=>import(&#39;../pages/stu1.vue&#39;)
                    }
                ]
            }

    Write stu1 component

    <template>
    stu1
    </template>
     
    <script setup>
     
    </script>
     
    <style scoped lang="less">
     
    </style>

    Write stu2 component

    <template>
    stu2
    </template>
     
    <script setup>
     
    </script>
     
    <style scoped lang="less">
     
    </style>

    Display subcomponents in the student component

    <template>
        学生组件
        <div>{{data.query}}</div>
        子组件展示
        <router-view></router-view>
        <router-link to="/student/stu1">到stu1</router-link>
        <router-link to="/student/stu2">到stu2</router-link>
    </template>
     
    <script setup>
    import { reactive } from &#39;vue&#39;;
    import {useRouter,useRoute} from &#39;vue-router&#39;
    const route=useRoute()
    let data=reactive({
        query: route.query
    })
    </script>

    By using router -link for routing jump, you can also jump through programmatic routing

    to="/student/stu1" You need to use the complete path to jump

    Show results

    How to configure Vue3 routing, perform route jumps and pass parameters?

    The above is the detailed content of How to configure Vue3 routing, perform route jumps and pass parameters?. For more information, please follow other related articles on the PHP Chinese website!

    Statement
    This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
    Frontend Development with Vue.js: Advantages and TechniquesFrontend Development with Vue.js: Advantages and TechniquesMay 03, 2025 am 12:02 AM

    Reasons for Vue.js' popularity include simplicity and easy learning, flexibility and high performance. 1) Its progressive framework design is suitable for beginners to learn step by step. 2) Component-based development improves code maintainability and team collaboration efficiency. 3) Responsive systems and virtual DOM improve rendering performance.

    Vue.js vs. React: Ease of Use and Learning CurveVue.js vs. React: Ease of Use and Learning CurveMay 02, 2025 am 12:13 AM

    Vue.js is easier to use and has a smooth learning curve, which is suitable for beginners; React has a steeper learning curve, but has strong flexibility, which is suitable for experienced developers. 1.Vue.js is easy to get started with through simple data binding and progressive design. 2.React requires understanding of virtual DOM and JSX, but provides higher flexibility and performance advantages.

    Vue.js vs. React: Which Framework is Right for You?Vue.js vs. React: Which Framework is Right for You?May 01, 2025 am 12:21 AM

    Vue.js is suitable for fast development and small projects, while React is more suitable for large and complex projects. 1.Vue.js is simple and easy to learn, suitable for rapid development and small projects. 2.React is powerful and suitable for large and complex projects. 3. The progressive features of Vue.js are suitable for gradually introducing functions. 4. React's componentized and virtual DOM performs well when dealing with complex UI and data-intensive applications.

    Vue.js vs. React: A Comparative Analysis of JavaScript FrameworksVue.js vs. React: A Comparative Analysis of JavaScript FrameworksApr 30, 2025 am 12:10 AM

    Vue.js and React each have their own advantages and disadvantages. When choosing, you need to comprehensively consider team skills, project size and performance requirements. 1) Vue.js is suitable for fast development and small projects, with a low learning curve, but deep nested objects can cause performance problems. 2) React is suitable for large and complex applications, with a rich ecosystem, but frequent updates may lead to performance bottlenecks.

    Vue.js vs. React: Use Cases and ApplicationsVue.js vs. React: Use Cases and ApplicationsApr 29, 2025 am 12:36 AM

    Vue.js is suitable for small to medium-sized projects, while React is suitable for large projects and complex application scenarios. 1) Vue.js is easy to use and is suitable for rapid prototyping and small applications. 2) React has more advantages in handling complex state management and performance optimization, and is suitable for large projects.

    Vue.js vs. React: Comparing Performance and EfficiencyVue.js vs. React: Comparing Performance and EfficiencyApr 28, 2025 am 12:12 AM

    Vue.js and React each have their own advantages: Vue.js is suitable for small applications and rapid development, while React is suitable for large applications and complex state management. 1.Vue.js realizes automatic update through a responsive system, suitable for small applications. 2.React uses virtual DOM and diff algorithms, which are suitable for large and complex applications. When selecting a framework, you need to consider project requirements and team technology stack.

    Vue.js vs. React: Community, Ecosystem, and SupportVue.js vs. React: Community, Ecosystem, and SupportApr 27, 2025 am 12:24 AM

    Vue.js and React each have their own advantages, and the choice should be based on project requirements and team technology stack. 1. Vue.js is community-friendly, providing rich learning resources, and the ecosystem includes official tools such as VueRouter, which are supported by the official team and the community. 2. The React community is biased towards enterprise applications, with a strong ecosystem, and supports provided by Facebook and its community, and has frequent updates.

    React and Netflix: Exploring the RelationshipReact and Netflix: Exploring the RelationshipApr 26, 2025 am 12:11 AM

    Netflix uses React to enhance user experience. 1) React's componentized features help Netflix split complex UI into manageable modules. 2) Virtual DOM optimizes UI updates and improves performance. 3) Combining Redux and GraphQL, Netflix efficiently manages application status and data flow.

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Atom editor mac version download

    Atom editor mac version download

    The most popular open source editor

    MinGW - Minimalist GNU for Windows

    MinGW - Minimalist GNU for Windows

    This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Safe Exam Browser

    Safe Exam Browser

    Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.