詳解vue?route介紹、基本使用、嵌套路由
前言
想要學習完整內(nèi)容請關(guān)注主頁的專欄————>Vue學習
本次的代碼段是結(jié)合體,被我分開發(fā)文,我以在看代碼段時,已經(jīng)截圖展示,所看部分
一、介紹、安裝
1.定義
vue-router是Vue.js官方的路由插件,它和vue.js是深度集成的,適合用于構(gòu)建單頁面應(yīng)用。
路由:route 一組key-v的對應(yīng)關(guān)系(路徑的改變對應(yīng)的組件進行切換)
路由器:router 多個路由需要路由器管理
為了實現(xiàn)單頁面應(yīng)用
2.安裝
npm i vue-router@3 安裝3版本
如果使用 vue ui 就沒有以下的操作,因為在創(chuàng)建項目的時候已經(jīng)配置好了
1:在src根目錄創(chuàng)建router目錄,在目錄中創(chuàng)建index.js,代碼如下:
import Vue from 'vue';
//導入vue-router
import VueRouter from 'vue-router'
//應(yīng)用插件
Vue.use(VueRouter)
//創(chuàng)建router規(guī)則對象
const routes = [
]
//創(chuàng)建router
const router = new VueRouter({
routes
})
//導出router
export default router2:main.js 中進行掛載
import Vue from 'vue'
import App from './App.vue'
import router from './router'
Vue.config.productionTip = false
new Vue({
router,
render: h => h(App)
}).$mount('#app')二、基本使用(代碼后賦)
以下例子展現(xiàn)路由的基本使用
css樣式已經(jīng)寫好了,直接實現(xiàn)路由效果
展示效果
首先學習的效果





代碼(看對應(yīng)的代碼段) app.vue代碼,此代碼含有樣式
<template>
<div id="root">
<div class="main">
<div class="header">
<h1>路由的演示</h1>
<button @click="back">后退</button>
</div>
</div>
<div class="main">
<div class="left">
<ul>
<li><router-link to="/about" active-class="hover">公司簡介</router-link></li>
<li><router-link to="/contaactus" active-class="hover">聯(lián)系方式</router-link></li>
<li><router-link to="/persons" active-class="hover">公司人員</router-link></li>
</ul>
</div>
<div class="right">
<router-view></router-view>
</div>
<div style="clear: both;"></div>
</div>
</div>
</template>
<script>
export default {
name:'App',
methods: {
back(){
this.$router.back()
}
},
components:{
},
}
</script>
<style>
.c{
clear: both;
}
*{
margin: 0px;
padding: 0px;
}
li{
list-style: none;
}
a{text-decoration: none;}
.main{width: 800px;margin: auto;}
.header{box-sizing: border-box;padding: 20px;border:1px solid #666;}
.left{
height: 500px;
border: 1px solid #666;
width: 200px;
float: left;
}
.left li{
height: 50px;
line-height: 50px;
text-align: center;
border-bottom: 1px solid #666;
width: 100%;
}
.left li a{
color: #333;display: block;
}
.left li a.hover{
background: blue;color: #fff;
}
.right{float: right;
border:1px solid #61DAFB;
width: 590px;
height: 500px;
}
.nav li{
float: left;
}
.nav li a{
width: 150px;
text-align: center;
height: 40px;line-height: 40px;
text-align: center;
border:1px solid #000000;
display: block;
}
.nav li a.hover{
background: #0000FF;color: #fff;
}
</style>三個路由組件的代碼
about
<template>
<div>
<!-- <div class="left"> -->
<ul class="nav">
<li><router-link to="/about/year" active-class="hover">創(chuàng)建年份</router-link></li>
<li><router-link to="/about/people" active-class="hover">創(chuàng)建人</router-link></li>
</ul>
<!-- </div> -->
<keep-alive include="People">
<router-view class="c"></router-view>
</keep-alive>
</div>
</template>
<script>
export default {
name: 'About',
data() {
return {
};
},
mounted() {
},
methods: {
},
};
</script>
<style scoped>
</style>ContaactUs
<template>
<div>
聯(lián)系方式
</div>
</template>
<script>
export default {
name: 'ContaactUs',
data() {
return {
};
},
mounted() {
},
methods: {
},
};
</script>
<style scoped>
</style>persons
<template>
<div>
<ul >
<li v-for="item in persons" :key="item.id">
<router-link :to="`/persons/show/${item.id}/${item.realname}`">姓名:{{item.realname}}</router-link>
<!-- <router-link :to="`/persons/show/?id=${item.id}&realname=${item.realname}`">姓名:{{item.realname}}</router-link> -->
<!-- <router-link :to="{name:'show',query:{id:item.id,realname:item.realname}}">姓名:{{item.realname}}</router-link> -->
<button @click="push(item)">點擊跳轉(zhuǎn)</button>
</li>
</ul>
<hr>
<router-view></router-view>
</div>
</template>
<script>
export default {
name:'Persons',
data(){
return{
persons:[
{id:1,realname:'張三'},
{id:2,realname:'李四'},
{id:3,realname:'王五'},
{id:4,realname:'趙六'}
]
}
},
methods: {
push(item){
this.$router.push(`/persons/show/${item.id}/${item.realname}`)
},
},
}
</script>
<style>
</style>router
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import About from '../pages/About'
import ContaactUs from '../pages/ContaactUs'
import Persons from '../pages/Persons'
// import Show from '../pages/Show'
// import Profile from '../pages/Profile'
// import People from '../pages/People'
const routes = [
{
path:'/about',
component:About,
children:[
// {name:'profile',path:'/about/year',component:Profile,meta:{isAuth:true}},
// {name:'people',path:'/about/people',component:People,meta:{isAuth:true}},
// {
// path:'/about',
// redirect:'/about/year'
// },
]},
{
path:'/contaactus',
component:ContaactUs
},
{
path:'/persons',
component:Persons,
// children:[
// {
// path:'show/:id/:realname',component:Show,props:true
// // name:'show', path:'show',component:Show
// }
// ]
},
{
path:'/',
redirect:'/about'
},
]
const router = new VueRouter({
mode:'history',
routes
})
// router.beforeEach((to,from,next)=>{
// if(to.name=="people" || to.name=="profile"){
// if(localStorage.getItem("token")=="123"){
// next();
// }
// }else{
// next();
// }
// })
// router.beforeEach((to,from,next)=>{
// if(to.meta.isAuth){
// if(localStorage.getItem("token")=="123"){
// next();
// }
// }else{
// next();
// }
// })
export default router以上就能實現(xiàn),視屏上的的切換的路由效果,如果有不懂的,私信問我,源碼私聊免費提供
三、嵌套路由
1.布局邏輯
嵌套路由在,最開始的路由下,加入路由

在about路由組件中

再次創(chuàng)建兩個路由組件,點擊是,獲得相對應(yīng)的內(nèi)容,實現(xiàn)路由效果

2.效果展示

3.代碼
about
<template>
<div>
<!-- <div class="left"> -->
<ul class="nav">
<li><router-link to="/about/year" active-class="hover">創(chuàng)建年份</router-link></li>
<li><router-link to="/about/people" active-class="hover">創(chuàng)建人</router-link></li>
</ul>
<!-- </div> -->
<keep-alive include="People">
<router-view class="c"></router-view>
</keep-alive>
</div>
</template>
<script>
export default {
name: 'About',
data() {
return {
};
},
mounted() {
},
methods: {
},
};
</script>
<style scoped>
</style>兩個路由組件
Profile
<template>
<div>
2002 08-20
</div>
</template>
<script>
export default {
name:'Profile',
// beforeDestroy () {
// console.log('已銷毀');
// },
}
</script>
<style>
</style>People
<template>
<div>
<span>傅小余</span> <input type="text">
</div>
</template>
<script>
export default {
name:"People",
// beforeDestroy () {
// console.log('已銷毀');
// },
}
</script>
<style>
</style>四、注意
這里我都使用到了默認路徑,所以頁面點開就會有展示效果
代碼如下
第一個里面的默認
{
path:'/',
redirect:'/about'
},第二個
{
path:'/about',
redirect:'/about/year'
},到此這篇關(guān)于詳解vue route介紹、基本使用、嵌套路由的文章就介紹到這了,更多相關(guān)vue route嵌套路由內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決Vue使用百度地圖BMapGL內(nèi)存泄漏問題?Out?of?Memory
這篇文章主要介紹了解決Vue使用百度地圖BMapGL內(nèi)存泄漏問題?Out?of?Memory,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
Vue2.0利用vue-resource上傳文件到七牛的實例代碼
本篇文章主要介紹了Vue2.0利用vue-resource上傳文件到七牛的實例代碼,具有一定的參考價值,有興趣的可以了解一下2017-07-07
基于Ant-design-vue的Modal彈窗 封裝 命令式與Hooks用法
這篇文章主要給大家介紹了基于Ant-design-vue的Modal彈窗封裝命令式與Hooks用法,文中有詳細的代碼示例,具有一定的參考價值,感興趣的同學可以借鑒閱讀2023-06-06
vue項目如何讓局域網(wǎng)ip訪問配置設(shè)置
這篇文章主要介紹了vue項目如何讓局域網(wǎng)ip訪問配置設(shè)置,具有很好的參考價值,希望對大家有所幫助。2022-09-09

