Vue-Router基礎(chǔ)學習筆記(小結(jié))
vue-router是一個插件包,要先用npm進行安裝
1、安裝vue-router
npm install vue-router yarn add vue-router
2、引入注冊vue-router
import Vue from 'vue' import VueRouter from 'vue-router' Vue.use(VueRouter)
3、鏈接跳轉(zhuǎn)
<router-link to='/home'></router-link> //你可以在template中使用它實現(xiàn)一個可點擊跳轉(zhuǎn)到home.vue的 a 標簽
this.$router.push('/about'); //在methods方法中跳轉(zhuǎn)到about頁面
this.$router.go('-1'); //在js中返回上一個頁面
4、經(jīng)常用到
this.$route.params.name //在js中獲取路由的參數(shù) .router-link-active //當前選中路由的匹配樣式 $route.query //獲取查詢參數(shù) $route.hash //哈希
5、路由配置
export default new Router({
routes:[
{ //第一層是頂層路由,頂層路由中的router-view中顯示被router-link選中的子路由
path:'/',
name:'Home',
component:'Home'
},{
path:'/user/:id', //www.xxx.com/user/cai
name:'user', //:id是動態(tài)路徑參數(shù)
component:'user',
children:[
{
path:'userInfo', //www.xxx.com/user/cai/userInfo
component:'userInfo' //子路由將渲染到父組件的router-view中
},{
path:'posts',
component:'posts'
}
]
}
]
})
Vue.use(Router);
6、路由參數(shù)方式變化時,重新發(fā)出請求并更新數(shù)據(jù)
//比如:用戶一切換到用戶二, 路由參數(shù)改變了,但組件是同一個,會被復用 // 從 /user/cai 切到 /user/wan
在User組件中:
//方法1:
watch:{
'$route'(to,from){
//做點什么,比如:更新數(shù)據(jù)
}
}
//方法二:
beforeRouteUpdate(to,from,next){
//同上
}
7、編程式導航
router.push({name:'user',params:{userId:'123'}}) //命名路由導航到user組件
<router-link :to='{name:'user',params:{userId:'123'}}'>用戶</router-link>
router.push({path:'register',query:{plan:'cai'}}) //query查詢參數(shù)
router.push({path:`/user/${userId}`}) //query
router.push(location,onComplete,onAbort)
router.replace() //替換
router.go(-1)
8、命名視圖
//當前組件中只有一個 router-view 時,子組件默認渲染到這里
<router-view class='default'></router-view>
<router-view class='a' name='left'></router-view>
<router-view class='b' name='main'></router-view>
routes:[
{
path:'/',
components:{
default:header,
left:nav,
main:content //content組件會渲染在name為main的router-view中
}
}
]
//嵌套命名視圖就是:子路由+命名視圖
9、重定向與別名
const router = new VueRouter({
routes: [
{ path: '/a', redirect: '/b' },
{ path: '/b', redirect: { name: 'foo' }}, //命名路由方式
{ path: '/c', redirect: to => { //動態(tài)返回重定向目標
// 方法接收 目標路由 作為參數(shù)
// return 重定向的 字符串路徑/路徑對象
}}
]
})
const router = new VueRouter({
routes: [
{ path: '/a', component: A, alias: '/b' } //別名:當訪問 /b 時也會使用A組件
]
})
10、路由組件傳參
const User={
props:['id'],
template:`<div>{{id}}</div>`
}
const router = new VueRouter({
routes: [
{ path: '/user/:id', component: User, props: true },
// 對于包含命名視圖的路由,你必須分別為每個命名視圖添加 `props` 選項:
{
path: '/user/:id',
components: { default: User, sidebar: Sidebar },
props: { default: true, sidebar: false }
}
]
})
11、HTML5的History模式下服務(wù)端配置
const router = new VueRouter({
mode: 'history',
routes: [
{ path: '*', component: 404}
]
})
后端配置:
//Nginx
location / {
try_files $uri $uri/ /index.html;
}
//Apache
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
//Node.js
const http = require('http')
const fs = require('fs')
const httpPort = 80
http.createServer((req, res) => {
fs.readFile('index.htm', 'utf-8', (err, content) => {
if (err) {
console.log('無法打開index.htm頁面.')
}
res.writeHead(200, {
'Content-Type': 'text/html; charset=utf-8'
})
res.end(content)
})
}).listen(httpPort, () => {
console.log('打開: http://localhost:%s', httpPort)
})
//使用了Node.js的Express
[使用中間件][1]
解耦合
routes: [
{ path: '/user/:id', component: User, props: true },
// 對于包含命名視圖的路由,你必須分別為每個命名視圖添加 `props` 選項:
{
path: '/user/:id',
components: { default: User, sidebar: Sidebar },
props: { default: true, sidebar: false }
}
]
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue路由傳參的基本實現(xiàn)方式小結(jié)【三種方式】
這篇文章主要介紹了vue路由傳參的基本實現(xiàn)方式,結(jié)合實例形式總結(jié)分析了vue.js路由傳參的三種實現(xiàn)方式,包括params顯示傳參、不顯示參數(shù)以及query顯示參數(shù)傳參,需要的朋友可以參考下2020-02-02
vue?elementui?實現(xiàn)搜索欄子組件封裝的示例代碼
這篇文章主要介紹了vue?elementui?搜索欄子組件封裝,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06
Vue動態(tài)創(chuàng)建注冊component的實例代碼
這篇文章主要給大家介紹了關(guān)于Vue動態(tài)創(chuàng)建注冊component的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Vue具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-06-06
vite.config.ts配置之自動導入element-puls方式
這篇文章主要介紹了vite.config.ts配置之自動導入element-puls方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10
vue關(guān)于this.$refs.tabs.refreshs()刷新組件方式
這篇文章主要介紹了vue關(guān)于this.$refs.tabs.refreshs()刷新組件方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
關(guān)于vue利用postcss-pxtorem進行移動端適配的問題
這篇文章主要介紹了關(guān)于vue利用postcss-pxtorem進行移動端適配的問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11

