本篇文章给大家带来的内容是关于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路由器的使用方法总结(附代码)
vue.js中路由器的配置方法介绍
以上就是vue.js中二级路由和三级路由的代码解析的详细内容。