温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

VUE多层路由嵌套实现代码

发布时间:2020-09-15 02:06:35 来源:脚本之家 阅读:183 作者:WakMeUp 栏目:web开发

本文实例为大家分享了VUE多层路由嵌套的具体代码,供大家参考,具体内容如下

先看看效果图:

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()方法来挂载。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI