内容简介:若希望在 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
雖然是輸入 /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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
算法心得:高效算法的奥秘(原书第2版)
(美)Henry S. Warren, Jr. / 爱飞翔 / 机械工业出版社 / 2014-3 / 89.00
【编辑推荐】 由在IBM工作50余年的资深计算机专家撰写,Amazon全五星评价,算法领域最有影响力的著作之一 Google公司首席架构师、Jolt大奖得主Hoshua Bloch和Emacs合作创始人、C语言畅销书作者Guy Steele倾情推荐 算法的艺术和数学的智慧在本书中得到了完美体现,书中总结了大量高效、优雅和奇妙的算法,并从数学角度剖析了其背后的原理 【读者评价......一起来看看 《算法心得:高效算法的奥秘(原书第2版)》 这本书的介绍吧!