詳解vue?route介紹、基本使用、嵌套路由
前言
想要學(xué)習(xí)完整內(nèi)容請關(guān)注主頁的專欄————>Vue學(xué)習(xí)
本次的代碼段是結(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'; //導(dǎo)入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 }) //導(dǎo)出router export default router
2: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)路由效果
展示效果
首先學(xué)習(xí)的效果
代碼(看對應(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 elementUI使用tabs與導(dǎo)航欄聯(lián)動
這篇文章主要為大家詳細介紹了vue elementUI使用tabs與導(dǎo)航欄聯(lián)動,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-06-06vue如何移動到指定位置(scrollIntoView)親測避坑
這篇文章主要介紹了vue如何移動到指定位置(scrollIntoView)親測避坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05vue菜單欄聯(lián)動內(nèi)容頁面tab的實現(xiàn)示例
本文主要介紹了vue菜單欄聯(lián)動內(nèi)容頁面tab的實現(xiàn)示例,左側(cè)菜單欄與右側(cè)內(nèi)容部分聯(lián)動,當點擊左側(cè)的菜單,右側(cè)會展示對應(yīng)的tab,具有一定的參考價值,感興趣的可以了解一下2024-01-01詳解如何從零開始搭建Express+Vue開發(fā)環(huán)境
這篇文章主要介紹了詳解如何從零開始搭建Express+Vue開發(fā)環(huán)境,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07Vue?Echarts實現(xiàn)實時大屏動態(tài)數(shù)據(jù)顯示
同大多數(shù)的前端框架一樣,先讀官網(wǎng)的使用方法。學(xué)會基本使用后,在實例中找到自己想要demo。拿過來改一改,一個echarts圖表就形成,畢竟人家做就是為了方便使用,這篇文章主要介紹了Vue?Echarts實現(xiàn)實時大屏動態(tài)數(shù)據(jù)顯示2022-10-10Vue中的this.$options.data()和this.$data用法說明
這篇文章主要介紹了Vue中的this.$options.data()和this.$data用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07vue-router解決相同路徑跳轉(zhuǎn)報錯的問題
這篇文章主要介紹了vue-router解決相同路徑跳轉(zhuǎn)報錯的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04