加入收藏 | 设为首页 | 会员中心 | 我要投稿 汽车网 (https://www.0577qiche.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

VueRouter 重定向到路由地址

发布时间:2023-04-19 09:58:02 所属栏目:教程 来源:
导读:重定向也是通过 routes 配置来完成,可以配置路由重定向到具体路由地址、具名路由或者动态返回重定向目标。

重定向到路由地址

通过属性 redirect 指定重定向的路由地址:

const router = new VueRouter({
重定向也是通过 routes 配置来完成,可以配置路由重定向到具体路由地址、具名路由或者动态返回重定向目标。

重定向到路由地址

通过属性 redirect 指定重定向的路由地址:

const router = new VueRouter({
  routes: [
    { path: '/a', redirect: '/b' }
  ]
})
示例:

<!DOCTYPE html>
<html lang="en">
<head>
  <Meta charset="UTF-8">
  <Meta name="viewport" content="width=device-width, initial-scale=1.0">
  <Meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <div>
      <router-link to="/index">首页</router-link>
      <router-link to="/article">文章</router-link>
    </div>
    <router-view></router-view>
  </div>
</body>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<script type="text/javascript">
const Index = Vue.component('index', {
  template: '<div>Hello,欢迎使用网学习 Vue 教程!</div>',
})
const Article = Vue.component('myArticle', {
  template: `<ul><li>1. Vue 计算属性的学习</li><li>2. React 基础学习</li></ul>`,
})
const routes = [
  { path: '/', redirect: '/index' },
  { path: '/index', component: Index },
  { path: '/article', component: Article }
]
const router = new VueRouter({
  routes: routes
})
var vm = new Vue({
  el: '#app',
  router: router,
  data() {
    return {}
  }
})
</script>
</html>
代码解释:
HTML 代码第 12-13 行,我们定义了两个跳转链接。
HTML 代码第 15 行,我们使用 <router-view></router-view> 组件来渲染匹配组件。
JS 代码第 5-7 行,我们定义了组件 Index。
JS 代码第 9-11 行,我们定义了组件 Article。
JS 代码第 13-17 行,我们定义了路由数组:

根路由,地址为 ‘/’,重定向到路由地址 ‘/index’。
首页路由,地址为 ‘/index’,匹配组件 Index。
文章路由,地址为 ‘/article’,匹配组件 Article。
JS 代码第 19-21 行,创建 router 实例,然后传 routes 配置。
JS 代码第 25 行,通过 router 配置参数注入路由。

重定向到具名路由

通过属性 redirect 重定向到具名路由:

const router = new VueRouter({
  routes: [
    { path: '/a', redirect: {name: 'name'} }
  ]
})
示例:

<!DOCTYPE html>
<html lang="en">
<head>
  <Meta charset="UTF-8">
  <Meta name="viewport" content="width=device-width, initial-scale=1.0">
  <Meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <div>
      <router-link to="/index">首页</router-link>
      <router-link to="/article">文章</router-link>
    </div>
    <router-view></router-view>
  </div>
</body>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<script type="text/javascript">
const Index = Vue.component('index', {
  template: '<div>Hello,欢迎使用网学习 Vue 教程!</div>',
})
const Article = Vue.component('myArticle', {
  template: `<ul><li>1. Vue 计算属性的学习</li><li>2. React 基础学习</li></ul>`,
})
const routes = [
  { path: '/', redirect: {name: 'index'} },
  { path: '/index', name: 'index', component: Index },
  { path: '/article', name: 'article', component: Article }
]
const router = new VueRouter({
  routes: routes
})
var vm = new Vue({
  el: '#app',
  router: router,
  data() {
    return {}
  }
})
</script>
</html>
代码解释:
HTML 代码第 12-13 行,我们定义了两个跳转链接。
HTML 代码第 15 行,我们使用 <router-view></router-view> 组件来渲染匹配组件。
JS 代码第 5-7 行,我们定义了组件 Index。
JS 代码第 9-11 行,我们定义了组件 Article。
JS 代码第 13-17 行,我们定义了路由数组:

根路由,地址为 ‘/’,重定向到具名路由 ‘index’。
首页路由,地址为 ‘/index’,匹配组件 Index。
文章路由,地址为 ‘/article’,匹配组件 Article。
JS 代码第 19-21 行,创建 router 实例,然后传 routes 配置。
JS 代码第 25 行,通过 router 配置参数注入路由。

动态返回重定向目标

属性 redirect 可以接收一个方法,动态返回重定向目标:

const router = new VueRouter({
  routes: [
    { path: '/a', redirect: to => {
      // 方法接收 目标路由 作为参数
      // return 重定向的 字符串路径/路径对象
    }}
  ]
})
示例:

<!DOCTYPE html>
<html lang="en">
<head>
  <Meta charset="UTF-8">
  <Meta name="viewport" content="width=device-width, initial-scale=1.0">
  <Meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <div>
      <router-link to="/index">首页</router-link>
      <router-link to="/article">文章</router-link>
    </div>
    <router-view></router-view>
  </div>
</body>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<script type="text/javascript">
const Index = Vue.component('index', {
  template: '<div>Hello,欢迎使用网学习 Vue 教程!</div>',
})
const Article = Vue.component('myArticle', {
  template: `<ul><li>1. Vue 计算属性的学习</li><li>2. React 基础学习</li></ul>`,
})
const routes = [
{ path: '/', redirect: to => {
  if(Math.random() > ) {
    return '/index'
  }else {
    return {
      name: 'article'
    }
  }
}},
  { path: '/index', name: 'index', component: Index },
  { path: '/article', name: 'article', component: Article }
]
const router = new VueRouter({
  routes: routes
})
var vm = new Vue({
  el: '#app',
  router: router,
  data() {
    return {}
  }
})
</script>
</html>
代码解释:
HTML 代码第 12-13 行,我们定义了两个跳转链接。
HTML 代码第 15 行,我们使用 <router-view></router-view> 组件来渲染匹配组件。
JS 代码第 5-7 行,我们定义了组件 Index。
JS 代码第 9-11 行,我们定义了组件 Article。
JS 代码第 13-25 行,我们定义了路由数组:

根路由,地址为 ‘/’,根据随机数的大小重定向到路由 ‘/index’ 或 ‘/article’。
首页路由,地址为 ‘/index’,匹配组件 Index。
文章路由,地址为 ‘/article’,匹配组件 Article。
JS 代码第 27-29 行,创建 router 实例,然后传 routes 配置。
JS 代码第 32 行,通过 router 配置参数注入路由。

(编辑:汽车网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章