路由器:路由器管理路由,VueRouter()用來創(chuàng)建路由器的構(gòu)造函數(shù)
new VueRouter({
//多個配置項
})
路由:其實就是一種映射關(guān)系,鍵值對K-V
K:path
V:前臺路由:組件
后臺路由:處理請求的回調(diào)函數(shù)
routers:[{
//一般路由
path: '/about',
component: About
},
{ //自動跳轉(zhuǎn)路由
path: '/',
redirect: '/about'
}
]
注冊路由器:
import router from './router'
new Vue({
router
})
使用路由組件標(biāo)簽:
1.<router-link>:用來生成路由鏈接
<router-link to='/xxx'>Go To XXX</router-link>
2.<router-view>:用來顯示當(dāng)前路由組件界面
<router-view></router-view>
點擊路由鏈接本身菩貌,并不向后臺發(fā)送ajax請求,只是更新顯示不同的路由組件柄延,
但是路由組件內(nèi)部可能會向后臺發(fā)送ajax請求翻屈,動態(tài)獲取數(shù)據(jù)
總結(jié):編寫使用路由的三步
1.定義路由組件
2.注冊路由
3.使用路由:<router-link><router-view>
嵌套路由:
配置嵌套路由
routers:[{
//一般路由
path: '/about',
component: About
},
{
path: '/home', //path最左側(cè)的/表示根路徑
component: Home,
children: [{
path: '/home/news',
component: News
},
{
path: 'message', //簡化寫法
component: Message
},
{
path: '',
redirect: '/home/news'
}]
},
{ //自動跳轉(zhuǎn)路由
path: '/',
redirect: '/about'
}
]
this有兩個常用的屬性诲泌,this.router
this.router:代表路由器,是一個功能的對象敷扫,里面包含了一些方法哀蘑,操作路由的方法,如push()葵第,replace()绘迁,back()等