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

How is redirection implemented in Vue Router?

王林
Release: 2023-07-21 09:16:45
Original
1992 people have browsed it

Vue Router is the routing manager officially recommended by Vue.js. It can help us implement the navigation function of single-page applications. In Vue Router, redirection is a very common requirement, which can automatically jump users from one routing address to another.

In Vue Router, we can use the redirect attribute to implement redirection. The specific implementation steps are as follows:

  1. Install Vue Router in the Vue project, and introduce the Vue Router library into the project.
// main.js中
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

// 其他配置和组件导入
Copy after login
  1. Create a VueRouter instance and define routing rules in it.
const router = new VueRouter({
  routes: [
    {
      /* 原始路由地址 */
      path: '/origin',
      name: 'origin',
      component: OriginComponent
    },
    {
      /* 重定向的目标路由地址 */
      path: '/redirect',
      name: 'redirect',
      redirect: '/target'  // 重定向的目标路由地址
    },
    {
      /* 目标路由地址 */
      path: '/target',
      name: 'target',
      component: TargetComponent
    }
  ]
})
Copy after login
  1. Use the redirection function in Vue components.
<template>
  <div>
    <router-link to="/origin">原始页面</router-link>
    <router-link to="/redirect">重定向页面</router-link>
    <router-link to="/target">目标页面</router-link>
  </div>
</template>

<script>
export default {
  /* 组件的其他配置项 */
}
</script>
Copy after login

In the above code example, we defined two routing addresses /origin and /target, in /redirect## When the # address initiates a request, it will be automatically redirected to the /target address.

When the user clicks

redirect page, a response to /redirect## will be initiated. #The request for the address will then automatically jump to the /target address, and finally the TargetComponent component will be rendered. Through the above configuration, we successfully implemented the redirection function in Vue Router. Of course, in addition to the above simple redirection methods, Vue Router also provides more flexible configuration options to meet various complex redirection needs.

Summary: Redirection in Vue Router is achieved by using the

redirect

attribute in routing rules. By defining the original routing address and the target routing address, and configuring the redirect attribute in the target routing address, the redirection function can be implemented. In actual development, we can flexibly configure different types of redirection according to specific needs.

The above is the detailed content of How is redirection implemented in Vue Router?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!