深入简出vue路由——3.嵌套路由

本文通过一个具体的示例展示了如何使用Vue.js与Vue Router实现基于动态路径参数的导航及子路由的功能。具体包括了如何定义组件、配置路由、创建链接等关键步骤。

代码在这里

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <script src="https://unpkg.com/vue/dist/vue.js"></script>
  <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>

<body>
<div id="app">
  <p>
    <router-link to="/user/foo">/user/foo</router-link>
    <router-link to="/user/boo/kkk">/user/foo/kkk</router-link>
    <router-link to="/user/boo/qqq">/user/foo/qqq</router-link>
  </p>  
  <router-view></router-view>
</div>
<script type="text/javascript">
  const User = {
    template:`
      <div class="user">
          <h2>User {{$route.params.id}}</h2>
          <router-view></router-view>
      </div>
    `
  }
  const ccc = {
    template:'<h3>CCC</h3>'
  };
  const kkk = {
    template:'<h3>KKK</h3>'
  };
  const qqq = {
    template:'<h3>QQQ</h3>'
  }
  const router = new VueRouter({
    routes:[
      {
        path:'/user/:id',// 动态路径参数 以冒号开头
        component:User,
        children:[
          {path:'',component:ccc},
          {path:'kkk',component:kkk},
          {path:'qqq',component:qqq}
        ]
      }
    ]
  })
  const app = new Vue({
    router//ES6解构函数的写法相当于 router:router
  }).$mount('#app')
</script>
</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值