本文實例為大家分享了VUE多層路由嵌套的具體代碼,供大家參考,具體內容如下
先看看效果圖:

例如:在做系統時,主頁面有兩個功能【home】and【news】,在【home】下又分為登錄和注冊。
首先需要將各種模板進行抽離。定義模板
<template id="home"> //home模板,里面含子視口 <div> <router-link to="/home/login">登錄</router-link> <router-link to="/home/zhuce">注冊</router-link> <router-view></router-view> </div> </template> <template id="news"> //消息模板 <div>news</div> </template> <template id="login"> //home模板下的登錄 <div>this is login</div> </template> <template id="zhuce"> //home模板下的注冊 <div>this is zhuce</div> </template>
JS下配置路由
const home={template:"#home"};
const news={template:'#news'};
const login={template:'#login'};
const zhuce={template:'#zhuce'};
var rout=[
{path:'/',redirect:'/home'}, //重定向為home ,當html后面哈希值為空時,默認顯示home
{
path:'/home',
component:home, //模板注冊
redirect:'/home/login',//子視口的重定向 默認登錄
children:[
{path:'/home/login',component:login}, //配置子模板
{path:'/home/zhuce',component:zhuce}
]},
{path:'/news',component:news}
];
var router=new VueRouter({ //實例化一個vuerouter
routes:rout
});
const app = new Vue({
router
}).$mount('#app')
當Vue實例沒有el屬性時,則該實例尚沒有掛載到某個dom中;
假如需要延遲掛載,可以在之后手動調用vm.$mount()方法來掛載。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。