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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值