vue-router.html 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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>vue-router</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. <h1>Hello App !</h1>
  15. <p>
  16. <router-link to='/home'>Go to Home</router-link>
  17. <router-link to='/new'>Go to New</router-link>
  18. </p>
  19. <router-view></router-view>
  20. </div>
  21. <script type="text/javascript">
  22. //1.创建组件
  23. const Home={
  24. template:`<span>我是主页</span>`
  25. };
  26. const News={
  27. template:`<span>我是新闻</span>`
  28. };
  29. //2.配置路由
  30. const routes=[
  31. {
  32. path:'/home',component:Home
  33. },
  34. {
  35. path:'/new',component:News
  36. },
  37. //重定向.相当于404,
  38. {
  39. path:'*',redirect:'/home'
  40. }
  41. ];
  42. //3.生成路由实例
  43. const router=new VueRouter({
  44. routes
  45. });
  46. new Vue({
  47. el:'#router',
  48. router
  49. })
  50. </script>
  51. </body>
  52. </html>