


Let's talk about routing in Vue3 and briefly analyze routing configuration methods
This article will take you through routing in Vue3 and talk about the basic configuration of routing, dynamic routing configuration, routing mode, routing redirection, etc. I hope it will be helpful to you.
[Related recommendations: "vue.js Tutorial"]
Basic configuration of routing
1. Install the plug-in
npm install vue-router@next --save
2, create a routers.ts file
3, introduce components in routers.ts and configure the path.
import { createRouter,createWebHashHistory } from 'vue-router'; // 引入组件 import Home from './components/Home.vue'; import News from './components/News.vue'; import User from './components/User.vue'; const router = createRouter({ history: createWebHashHistory(), routes: [ {path: '/', component: Home}, {path: '/news', component: News}, {path: '/user', component: User}, ] }) export default router;
4. Mount the routing file to vue in main.ts.
import { createApp } from 'vue' import App from './App.vue' import routers from './routers'; // createApp(App).mount('#app') const app = createApp(App); app.use(routers); app.mount('#app');
5. After the component that uses routing mounts router-link through the router-view component or router-link
<template> <img alt="Vue logo" src="./assets/logo.png"> <ul> <li> <router-link to="/">首页</router-link> </li> <li> <router-link to="/news">新闻</router-link> </li> <li> <router-link to="/user">用户</router-link> </li> </ul> <router-view></router-view> </template>
, you only need to go to the page path corresponding to the component. Enter the specified route to complete the jump, and router-link implements routing in the form of a tag for jump.
Configuration of dynamic routing
Configure routing in routes.ts as follows, and configure dynamic routing through /:aid.
//配置路由 const router = createRouter({ history: createWebHashHistory(), routes: [ { path: '/', component: Home , alias: '/home' }, { path: '/news', component: News }, { path: '/user', component: User }, { path: '/newscontent/:aid', component: NewsContent }, ], })
When jumping through router-link, a template string and colon + to are required.
<ul> <li v-for="(item, index) in list" :key="index"> <router-link :to="`/newscontent/${index}`"> {{item}}</router-link> </li> </ul>
Get the value passed by dynamic routing through this.$route.params.
mounted(){ // this.$route.params 获取动态路由的传值 console.log(this.$route.params) }
If we want to achieve a value transfer similar to GET, we can use the following method
1. Configure the route as a normal route.
const router = createRouter({ history: createWebHashHistory(), routes: [ { path: '/', component: Home , alias: '/home' }, { path: '/news', component: News }, { path: '/user', component: User }, { path: '/newscontent', component: NewsContent }, ], })
2. Router-link jumps in the form of question marks.
<router-link :to="`/newscontent?aid=${index}`"> {{item}}</router-link>
3. Get the get value through this.$route.query.
console.log(this.$route.query);
Routing programmatic navigation (JS jump routing)
Just need to specify it through this.$router.push.
this.$router.push({ path: '/home' })
If you want to implement get value transfer, you can use the following method.
this.$router.push({ path: '/home', query: {aid: 14} }) }
Dynamic routing needs to use the following method.
this.$router.push({ path: '/home/123', // query: {aid: 14} })
Routing mode
Hash mode
The typical feature of Hash mode is that the page route contains a pound sign.
const router = createRouter({ history: createWebHashHistory(), routes: [ ..., ], })
HTML5 history mode
Introduces createWebHistory.
The history attribute in the router's configuration item is set to createWebHistory().
import { createRouter, createWebHistory } from 'vue-router' //配置路由 const router = createRouter({ history: createWebHistory(), routes: [ ... ], })
Note: After turning on HTML5 History mode, you need to configure pseudo-static when publishing to the server.
Configuring pseudo-static method:
https://router.vuejs.org/zh/guide/essentials/history-mode.html#Backend configuration example
Named routing
General situation
Configure the name attribute when defining the route
{ path: '/news', component: News,name:"news" }
Pass in the object to jump
<router-link :to="{name: 'news'}">新闻</router-link>
Pass the value through GET
When defining the route, configure the name attribute
{ path: '/newscontent', component: NewsContent, name: "content" },
Pass in the object including query
<li v-for="(item, index) in list" :key="index"> <router-link :to="{name: 'content',query: {aid: index}}"> {{item}}</router-link> </li>
Through dynamic Routing method
Define dynamic routing and specify the name attribute
{ path: '/userinfo/:id', name: "userinfo", component: UserInfo }
Pass in the object including params
<router-link :to="{name: 'userinfo',params: {id: 123}}">跳转到用户详情</router-link>
Programmatic routing
is very similar to the above method.
<button @click="this.$router.push({name: 'userinfo',params: {id: 666}})">点击跳转</button>
Route redirection
{ path: '', redirect: "/home" }, // 路由重定向 { path: '/home', component: Home },
Route alias
In the following example, accessing the people route is the same as accessing the alias route.
{ path: '/user', component: User, alias: '/people' }
alias can also be an array.
{ path: '/user', component: User, alias: ['/people','/u']}
Form of dynamic routing.
{ path: '/userinfo/:id', name: "userinfo", component: UserInfo, alias: '/u/:id' }
Nested routing
The application scenario of nested routing is generally on the navigation bar.
Define nested routing
{ path: '/user', component: User, children: [ { path: '', redirect: "/user/userlist" }, { path: 'userlist', component: UserList }, { path: 'useradd', component: UserAdd } ] }
router-link and router-view display content together
<div class="left"> <ul> <li> <router-link to="/user/userlist">用户列表</router-link> </li> <li> <router-link to="/user/useradd">增加用户</router-link> </li> </ul> </div> <div class="right"> <router-view></router-view> </div>
For more programming-related knowledge, please visit: Introduction to Programming! !
The above is the detailed content of Let's talk about routing in Vue3 and briefly analyze routing configuration methods. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











How to implement API routing in the Slim framework Slim is a lightweight PHP micro-framework that provides a simple and flexible way to build web applications. One of the main features is the implementation of API routing, allowing us to map different requests to corresponding handlers. This article will introduce how to implement API routing in the Slim framework and provide some code examples. First, we need to install the Slim framework. The latest version of Slim can be installed through Composer. Open a terminal and

ThinkPHP6 is a powerful PHP framework with convenient routing functions that can easily implement URL routing configuration; at the same time, ThinkPHP6 also supports a variety of routing modes, such as GET, POST, PUT, DELETE, etc. This article will introduce how to use ThinkPHP6 for routing configuration. 1. ThinkPHP6 routing mode GET method: The GET method is a method used to obtain data and is often used for page display. In ThinkPHP6, you can use the following

Apache Camel is an Enterprise Service Bus (ESB)-based integration framework that can easily integrate disparate applications, services, and data sources to automate complex business processes. ApacheCamel uses route-based configuration to easily define and manage integration processes. Key features of ApacheCamel include: Flexibility: ApacheCamel can be easily integrated with a variety of applications, services, and data sources. It supports multiple protocols, including HTTP, JMS, SOAP, FTP, etc. Efficiency: ApacheCamel is very efficient, it can handle a large number of messages. It uses an asynchronous messaging mechanism, which improves performance. Expandable

How to use routing to customize page switching animation effects in a Vue project? Introduction: In the Vue project, routing is one of the functions we often use. Switching between pages can be achieved through routing, providing a good user experience. In order to make page switching more vivid, we can achieve it by customizing animation effects. This article will introduce how to use routing to customize the page switching animation effect in the Vue project. Create a Vue project First, we need to create a Vue project. You can use VueCLI to quickly build

vue3的生命周期:1、beforeCreate;2、created;3、beforeMount;4、mounted;5、beforeUpdate;6、updated;7、beforeDestroy;8、destroyed;9、activated;10、deactivated;11、errorCaptured;12、getDerivedStateFromProps等等

Implementation method and experience summary of flexible configuration of routing rules in PHP Introduction: In Web development, routing rules are a very important part, which determines the corresponding relationship between URL and specific PHP scripts. In the traditional development method, we usually configure various URL rules in the routing file, and then map the URL to the corresponding script path. However, as the complexity of the project increases and business requirements change, it will become very cumbersome and inflexible if each URL needs to be configured manually. So, how to implement in PHP

In modern web applications, implementing web page navigation and routing is a very important part. Using JavaScript functions to implement this function can make our web applications more flexible, scalable and user-friendly. This article will introduce how to use JavaScript functions to implement web page navigation and routing, and provide specific code examples. Implementing web page navigation For a web application, web page navigation is the most frequently operated part by users. When a user clicks on the page

How to use routing to implement page jump in Vue? With the continuous development of front-end development technology, Vue.js has become one of the most popular front-end frameworks. In Vue development, page jump is an essential part. Vue provides VueRouter to manage application routing, and seamless switching between pages can be achieved through routing. This article will introduce how to use routing to implement page jumps in Vue, with code examples. First, install the vue-router plugin in the Vue project.
