首頁 > web前端 > js教程 > 主體

vue.js中二級路由與三級路由的程式碼解析

不言
發布: 2018-08-23 16:25:16
原創
2086 人瀏覽過

這篇文章帶給大家的內容是關於vue.js中二級路由和三級路由的程式碼解析 ,有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。

為什麼要用二級和三級路由?

當專案中有多個 時,就必須使用分級路由;

如果路由不分級,又有多個 ,全部都是定義為一級路由,那麼專案中的  的內容顯示就會混亂;這是不友善的;

如果對路由進行分級,那麼當觸發某個二級或三級路由時,此路由就會將對應元件內容傳給自己的父級路由元件裡面的,這樣就不會混亂;

一級路由被觸發時,自然會找自己所註冊的根元件的

二級路由

為一級路由新增一個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路由器的使用方法總結(附程式碼)

 vue.js中路由器的配置方法介紹

以上是vue.js中二級路由與三級路由的程式碼解析的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!