vue-router2.html 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  6. <title>嵌套路由</title>
  7. <meta name="description" content="">
  8. <meta name="keywords" content="">
  9. <script type="text/javascript" src='js/vue.min.js'></script>
  10. <script type="text/javascript" src='js/vue-router.js'></script>
  11. </head>
  12. <body>
  13. <div id='router'>
  14. <router-link to='/home'>Go to Home</router-link>
  15. <router-link to='/user'>Go to User</router-link>
  16. <router-view></router-view>
  17. </div>
  18. <script type="text/javascript">
  19. const Home={
  20. template:`<span>我是主页</span>`
  21. };
  22. const username={
  23. //$route.params: 表示冒号后面的一个对象
  24. template:`<span>我是{{$route.params}}</span>`
  25. };
  26. const News={
  27. template:`<div>
  28. <h3>我是用户</h3>
  29. <ul><li><router-link to='/user/sunseekers'>用户信息</router-link></li><ul>
  30. <router-view></router-view>
  31. </div>`
  32. };
  33. const routes=[
  34. {path:'/home',component:Home},
  35. {
  36. path:'/user',component:News,
  37. //配置子路由或者说是嵌套路由,只需要在嵌套或者父路由里面加一个
  38. //children[{path:'',component:}]就好,很形象,很好记
  39. children:[
  40. {path:':username',component:username}
  41. ]},
  42. {path:'*',redirect:'/home'}
  43. ];
  44. const router=new VueRouter({routes});
  45. new Vue({
  46. el:'#router',
  47. router
  48. })
  49. </script>
  50. </body>
  51. </html>