Vue Router 之 Redirect

栏目: 编程语言 · 发布时间: 4年前

内容简介:若希望在 Browser 輸入特定 Route 之後,卻自動轉址到其他網址,則要使用 Redirect。Vue 2.6.10Vue-router 3.0.3

若希望在 Browser 輸入特定 Route 之後,卻自動轉址到其他網址,則要使用 Redirect。

Version

Vue 2.6.10

Vue-router 3.0.3

Vue CLI 3.8.4

Redirect

Vue Router 之 Redirect

雖然是輸入 /about-us ,但會自動轉到 /about

By String

router.js

import Vue from 'vue';
import Router from 'vue-router';
import Home from './views/Home.vue';

Vue.use(Router);

export default new Router({
  routes: [
    {
      path: '/',
      name: 'home',
      component: Home
    },
    {
      path: '/about',
      name: 'about',
      component: () => import(/* webpackChunkName: "about" */ './views/About.vue')
    },
    {
      path: '/about-us',
      name: 'about-us',
      redirect: '/about'
    }
  ]
})

19 行

{
  path: '/about-us',
  name: 'about-us',
  redirect: '/about'
}

在 route object 加上 redirect property,使用 string 指定要轉址的網址。

By Named Route

{
  path: '/about-us',
  name: 'about-us',
  redirect: { name: 'about' }
}

Redirect 也可以搭配 named route,將 object 指定給 redirect property,並將 route 名稱指定給 name property。

By Function

{
  path: '/about-us',
  name: 'about-us',
  redirect: () => '/about',
}

Redirect 還可搭配 function,適合更複雜的邏輯。

Conclusion

  • Vue Router 提供了 string、named route 與 function 三種 redirect 方式,尤其是 function,除了適合更複雜的邏輯外,甚至可以使用 higher order function 與 closure 產生 redirect

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

构建之法

构建之法

邹欣 / 人民邮电出版社 / 2014-9 / 49.00元

内容简介: 软件工程牵涉的范围很广, 同时也是一般院校的同学反映比较空洞乏味的课程。 但是软件工程的技术对于投身IT 产业的学生来说是非常重要的。作者邹欣有长达20年的一线软件开发经验,他利用业余时间在数所高校进行了长达6年的软件工程教学实践,总结出了在16周的时间内让 同学们通过 “做中学 (Learning By Doing)” 掌握实用的软件工程技术的教学计划,并得到高校师生的积极反馈......一起来看看 《构建之法》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

SHA 加密
SHA 加密

SHA 加密工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具