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

Web design reference firefox default style

Although W3C has established some standards for HT...

My CSS framework - base.css (reset browser default style)

Copy code The code is as follows: @charset "...

Use of Linux crontab command

1. Command Introduction The contab (cron table) c...

Detailed explanation of HTML style tags and related CSS references

HTML style tag style tag - Use this tag when decl...

Mysql practical exercises simple library management system

Table of contents 1. Sorting function 2. Prepare ...

Detailed steps for implementing timeout status monitoring in Apache FlinkCEP

CEP - Complex Event Processing. The payment has n...

Beginner's guide to building a website ⑥: Detailed usage of FlashFXP

Today I will introduce the most basic functions of...

my.cnf (my.ini) important parameter optimization configuration instructions

MyISAM storage engine The MyISAM storage engine i...

The whole process of configuring reverse proxy locally through nginx

Preface Nginx is a lightweight HTTP server that u...

Detailed explanation of the role of static variables in MySQL

Detailed explanation of the role of static variab...

abbr mark and acronym mark

The <abbr> and <acronym> tags represen...

Summary of 4 ways to add users to groups in Linux

Preface Linux groups are organizational units use...

Nginx access log and error log parameter description

illustrate: There are two main types of nginx log...