Complete steps to use vue-router in vue3

Complete steps to use vue-router in vue3

Preface

Managing routing is an essential feature for most single-page applications. With the new version of Vue Router in alpha stage, we can already start to see how it works in the next version of Vue.

Many of the changes in Vue3 will slightly alter the way we access plugins and libraries, and that includes Vue Router.

1. Step 1: Install vue-router

npm install [email protected]

2. Step 2: main.js

Let's first compare the difference between main.js in vue2 and vue3: (the first one is vue2, the second one is vue3)

It can be clearly seen that the Vue object we often use in vue2 has "disappeared" in vue3 due to the direct use of the createApp method, but in fact the app created by the createApp method is a Vue object. The Vue.use() often used in vue2 can be replaced with app.use() in vue3 for normal use; in the main.js file of vue3, use vue-router to directly call the router with the app.use() method.

Note: import the route name exported by the route file from "corresponding to the relative path of the route file", the project directory is as follows (same for vue2 and vue3):

3. Routing File

import { createRouter, createWebHashHistory } from "vue-router"

const routes = [
    {
        path: '/',
        component: () => import('@/pages')             
    },
    {
        path: '/test1',
        name: "test1",
        component: () => import('@/pages/test1')   
    },
    {
        path: '/test2',
        name: "test2",
        component: () => import('@/pages/test2')   
    },
]
export const router = createRouter({
  history: createWebHashHistory(),
  routes: routes
})

export default router

4. app.vue

<template>
  <router-view></router-view>
</template>

<script>

export default {
  name: 'App',
  components:
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

4. Use (such as jump)

We introduce useRoute and useRouter where routing is needed (equivalent to $route and $router in vue2)

<script>
import { useRoute, useRouter } from 'vue-router'
export default {
  setup () {
    const route = useRoute()
    const router = useRouter()
    return {}
  },
}

Example: Page jump

<template>
  <h1>I am test1</h1>
  <button @click="toTest2">toTest2</button>
</template>
<script>
import { useRouter } from 'vue-router'
export default {
  setup () {
    const router = useRouter()
    const toTest2= (() => {
      router.push("./test2")
    })
    return {
      toTest2
    }
  },
}
</script>
<style scoped>
</style>

Summarize

This is the end of this article about vue3 using vue-router. For more relevant content about vue3 using vue-router, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Example of using router global guard in Vue to implement page interception
  • vue-router permission control (simple way)
  • vue-router controls the implementation of routing permissions
  • Vue3 uses vue-router and routing permission interception method

<<:  Solve the problem of installing Tenda U12 wireless network card driver on Centos7

>>:  Detailed explanation of MySQL Limit performance optimization and paging data performance optimization

Recommend

Solve the problem of running hello-world after docker installation

Installed Docker V1.13.1 on centos7.3 using yum B...

PostgreSQL materialized view process analysis

This article mainly introduces the process analys...

Bundling non-JavaScript static resources details

Table of contents 1. Custom import in packaging t...

Solve the problem of Docker starting Elasticsearch7.x and reporting an error

Using the Docker run command docker run -d -p 920...

JavaScript to achieve elastic navigation effect

This article shares the specific code for JavaScr...

How to change the encoding to utf-8 in mysql version 5.7 under windows

Preface I just started learning MySQL and downloa...

Master-slave synchronous replication configuration of MySQL database under Linux

The advantage of the master-slave synchronization...

Detailed steps to install docker in 5 minutes

Installing Docker on CentOS requires the operatin...

Vue implements three-level navigation display and hiding

This article example shares the specific code of ...

Explanation of building graph database neo4j in Linux environment

Neo4j (one of the Nosql) is a high-performance gr...

mysql8.0 windows x64 zip package installation and configuration tutorial

MySQL 8 Windows version zip installation steps (d...

Javascript scope and closure details

Table of contents 1. Scope 2. Scope Chain 3. Lexi...

MySQL installation and configuration methods and precautions under Windows platform

2.1、msi installation package 2.1.1、Installation I...

How to use vue.js to implement drag and drop function

Preface Adding drag and drop functionality is a g...