第一種方法:html
const router = new VueRouter({
routes: [
{ path: '/a', redirect: '/b' }
]
})
複製代碼
第二種方法:vue
const router = new VueRouter({
routes: [
{ path: '/a', redirect: { name: 'foo' }}
]
})
複製代碼
第三種方法:vue-router
const router = new VueRouter({
routes: [
{
path: '/a',
redirect: to =>{
const { hash, params, query } = to
if (query.to === 'foo') {
return { path: '/foo', query: null }
}else{
return '/b'
}
}
}
]
})
複製代碼
const router = new VueRouter({
routes: [
{
path: '*', redirect: {path: '/'}
}
]
})
複製代碼
<keep-alive :include="include">
<router-view></router-view>
</keep-alive>
複製代碼
其中include能夠是個數組,數組內容爲路由的name選項的值。編程
beforeRouteLeave(to,form,next)
。beforeEach(to,form,next)
守衛。beforeRouteUpdate(to,form,next)
守衛。beforeEnter(to,form,next)
路由獨享的守衛。beforeRouteEnter(to,form,next)
。beforeResolve(to,form,next)
解析守衛。afterEach(to,form)
鉤子。beforeRouteEnter(to,from,next){ next(vm =>{//經過vm訪問組件實例}) }
守衛中傳給 next 的回調函數。next()
:進入下一個路由。next(false)
:中斷當前的導航。next('/')
或next({ path: '/' })
: 跳轉到其餘路由,當前導航被中斷,進行新的一個導航。next()
嗎?不能夠,不接受next
的參數。數組
router.beforeEach
:全局前置守衛。router.beforeResolve
:全局解析守衛。router.afterEach
:全局後置鉤子。是beforeEnter
守衛瀏覽器
const router = new VueRouter({
routes: [
{
path: '/foo',
component: Foo,
beforeEnter: (to, from, next) => {
// ...
}
}
]
})
複製代碼
beforeRouteLeave
:在失活的組件裏調用離開守衛。beforeRouteUpdate
:在重用的組件裏調用。beforeRouteEnter
:在進入對應路由的組件建立前調用。beforeRouteEnter
導航守衛中能夠用this嗎?能夠經過傳一個回調給next來訪問組件實例。在導航被確認的時候執行回調,而且把組件實例做爲回調方法的參數。bash
beforeRouteEnter(to, from, next) {
next(vm => {
console.log(vm)
})
}
複製代碼
<router-link>
是Vue-Router的內置組件,在具備路由功能的應用中做爲聲明式的導航使用。服務器
<router-link>
有8個props,其做用是:app
to
:必填,表示目標路由的連接。當被點擊後,內部會馬上把to
的值傳到router.push()
,因此這個值能夠是一個字符串或者是描述目標位置的對象。<router-link to="home">Home</router-link>
<router-link :to="'home'">Home</router-link>
<router-link :to="{ path: 'home' }">Home</router-link>
<router-link :to="{ name: 'user', params: { userId: 123 }}">User</router-link>
<router-link :to="{ path: 'user', query: { userId: 123 }}">User</router-link>
複製代碼
注意path存在時params不起做用,只能用queryreplace
:默認值爲false,若設置的話,當點擊時,會調用router.replace()
而不是router.push()
,因而導航後不會留下 history 記錄。append
:設置 append 屬性後,則在當前 (相對) 路徑前添加基路徑。tag
:讓<router-link>
渲染成tag
設置的標籤,如tag:'li
,渲染結果爲<li>foo</li>
。active-class
:默認值爲router-link-active
,設置連接激活時使用的 CSS 類名。默認值能夠經過路由的構造選項 linkActiveClass 來全局配置。exact-active-class
:默認值爲router-link-exact-active
,設置連接被精確匹配的時候應該激活的 class。默認值能夠經過路由構造函數選項 linkExactActiveClass 進行全局配置的。exact
:是否精確匹配,默認爲false。<!-- 這個連接只會在地址爲 / 的時候被激活 -->
<router-link to="/" exact></router-link>
複製代碼
event
:聲明能夠用來觸發導航的事件。能夠是一個字符串或是一個包含字符串的數組,默認是click
。有兩種方法能夠監聽路由參數的變化,可是隻能用在包含<router-view />
的組件內。異步
watch: {
'$route'(to, from) {
//這裏監聽
},
},
複製代碼
beforeRouteUpdate (to, from, next) {
//這裏監聽
},
複製代碼
const router = new Router({
mode: 'history',
base: process.env.BASE_URL,
routes,
scrollBehavior(to, from, savedPosition) {
if (savedPosition) {
return savedPosition;
} else {
return { x: 0, y: 0 };
}
}
});
複製代碼
作個管理系統,頂部欄和左側菜單欄是全局通用的,那就應該放在父路由,而右下的頁面內容部分放在子路由。
路由有三種傳參方式,獲取方式各不相同。
meta
:路由元信息,寫在routes配置文件中。{
path: '/home',
name: 'home',
component: load('home'),
meta: {
title: '首頁'
},
},
複製代碼
獲取方式this.$route.meta.title
獲取this.$route.push({
path:'/home',
query:{
userId:123
}
})
複製代碼
瀏覽器地址:http://localhost:8036/home?userId=123
獲取方式:this.$route.query.userId
{
path: '/home/:userId',
name: 'home',
component: load('home'),
meta: {
title: '首頁'
},
},
複製代碼
const userId = '123'
this.$router.push({ name: 'home', params: { userId } })
複製代碼
注意用params傳參,只能用命名的路由(用name訪問),若是用path,params不起做用。 this.$router.push({ path: '/home', params: { userId }})
不生效。http://localhost:8036/home/123
this.$route.params.userId
由於在組件中使用 $route 會使之與其對應路由造成高度耦合,從而使組件只能在某些特定的 URL 上使用,限制了其靈活性,全部要解耦。
http://localhost:8036/home/123
URL上才能使用。const Home = {
template: '<div>User {{ $route.params.id }}</div>'
}
const router = new VueRouter({
routes: [
{ path: '/home/:id', component: Home }
]
})
複製代碼
route.params
將會被設置爲組件屬性。http://localhost:8036/home?id=123
,會把123傳給組件Home的props的id。const Home = {
props: ['id'],
template: '<div>User {{ id }}</div>'
}
const router = new VueRouter({
routes: [
{ path: '/home/:id', component: Home, props: true},
// 對於包含命名視圖的路由,你必須分別爲每一個命名視圖添加 `props` 選項:
{
path: '/home/:id',
components: { default: Home, sidebar: Sidebar },
props: { default: true, sidebar: false }
}
{ path: '/home/:id', component: Home, props: {id:123} },
{ path: '/home/:id', component: Home, props: (route) => ({ id: route.query.id }) },
]
})
複製代碼
<router-link/>
組件的屬性,設置連接激活時使用的 CSS 類名。默認值能夠經過路由的構造選項 linkActiveClass 來全局配置。
經過this.$router
來獲取
使用Router的實例方法addRoutes來實現動態加載路由,通常用來實現菜單權限。
使用時要注意,靜態路由文件中不能有404路由,而要經過addRoutes一塊兒動態添加進去。
const routes = [
{
path: '/overview',
name: 'overview',
component: () => import('@/views/account/overview/index'),
meta: {
title: '帳戶概覽',
pid: 869,
nid: 877
},
},
{
path: '*',
redirect: {
path: '/'
}
}
]
vm.$router.options.routes.push(...routes);
vm.$router.addRoutes(routes);
複製代碼
function load(component) {
//return resolve => require([`views/${component}`], resolve);
return () => import(`views/${component}`);
}
const routes = [
{
path: '/home',
name: 'home',
component: load('home'),
meta: {
title: '首頁'
},
},
]
複製代碼
<router-link :to="/home">
來跳轉router.push({ path: '/home' })
或replace方法router.replace({ path: '/home' })
因此要在Ngnix中將全部請求都轉發到index.html上就能夠了。
location / {
  try_files $uri $uri/ @router index index.html;
}
location @router {
rewrite ^.*$ /index.html last;
}
複製代碼