這篇文章帶給大家的內容是關於vue.js中二級路由和三級路由的程式碼解析 ,有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。
為什麼要用二級和三級路由?
當專案中有多個975b587bf85a482ea10b0a28848e78a4 時,就必須使用分級路由;
如果路由不分級,又有多個975b587bf85a482ea10b0a28848e78a4 ,全部都是定義為一級路由,那麼專案中的 975b587bf85a482ea10b0a28848e78a4 的內容顯示就會混亂;這是不友善的;
如果對路由進行分級,那麼當觸發某個二級或三級路由時,此路由就會將對應元件內容傳給自己的父級路由元件裡面的975b587bf85a482ea10b0a28848e78a4,這樣就不會混亂;
一級路由被觸發時,自然會找自己所註冊的根元件的975b587bf85a482ea10b0a28848e78a4
為一級路由新增一個children 屬性數組,並將二級路由放入;
path 屬性決定跳轉後url 網址列的顯示
//main.js //一级路由 import About from './components/about/About' //二级路由 import Contact from './components/about/Contact' import Delivery from './components/about/Delivery' import History from './components/about/History' import OrderingGuide from './components/about/OrderingGuide' const router= new VueRouter({ routes:[ {path:'/about',name:'about',component:About,children:[ {path:'/about/contsssssssssssssssact',name:'contactLink',component:Contact}, {path:'/history',name:'historyLink',component:History}, {path:'/',name:'deliveryLink',component:Delivery}, {path:'/orderingGuide',name:'orderingGuideLink',component:OrderingGuide}, ]}, {path:'*',redirect:'/'} ], mode:"history" });
與二級路由差不多
const router= new VueRouter({ routes:[ {path:'/',name:'home',component:Home}, {path:'/menu',name:'menu',component:Menu}, {path:'/admin',name:'admin',component:Admin}, {path:'/about',name:'about',component:About,redirect: {name:'contactLink'},children:[ //二级路由 {path:'/about/contact',name:'contactLink',component:Contact}, {path:'/history',name:'historyLink',component:History}, {path:'/delivery',name:'deliveryLink',component:Delivery}, {path:'/orderingGuide',name:'orderingGuideLink',component:OrderingGuide,redirect:{name:'phonelink'},children: [ //三级路由 {path:'/phone',name:'phonelink',component:Phone}, {path:'/name',name:'namelink',component:Name} ]}, ]}, {path:'/login',name:'login',component:Login}, {path:'/register',name:'register',component:Register}, {path:'*',redirect:'/'} ], mode:"history" });
相關推薦:
以上是vue.js中二級路由與三級路由的程式碼解析的詳細內容。更多資訊請關注PHP中文網其他相關文章!