routing.html 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <script src="../../../dist/vue.js"></script>
  2. <style type="text/css">
  3. body {
  4. padding: 20px;
  5. font-family: 'Helvetica Neue', Arial, sans-serif;
  6. }
  7. ul {
  8. list-style-type: none;
  9. padding: 0;
  10. }
  11. li {
  12. display: inline-block;
  13. margin-right: 10px;
  14. }
  15. a {
  16. color: #999;
  17. text-decoration: none;
  18. }
  19. a.current {
  20. color: blue;
  21. }
  22. .view {
  23. position: absolute;
  24. opacity: 1;
  25. -webkit-transition: all .2s ease;
  26. transition: all .2s ease;
  27. }
  28. .v-enter {
  29. opacity: 0;
  30. -webkit-transform: translate3d(30px, 0, 0);
  31. transform: translate3d(30px, 0, 0);
  32. }
  33. .v-leave {
  34. opacity: 0;
  35. -webkit-transform: translate3d(-30px, 0, 0);
  36. transform: translate3d(-30px, 0, 0);
  37. }
  38. </style>
  39. <div>
  40. <ul>
  41. <li v-repeat="routes">
  42. <a href="#!/{{$value}}" v-class="current:currentView == $value">{{$value}}</a>
  43. </li>
  44. </ul>
  45. <div v-view="currentView" class="view" v-with="global:data" v-transition>
  46. <p>Hello! {{msg}} {{global.test}}</p>
  47. </div>
  48. </div>
  49. <script>
  50. Vue.config('debug', true)
  51. Vue.component('home', {
  52. template: '<h1>Home</h1><div class="content"><content/></div>',
  53. created: function () {
  54. this.msg = "Home sweet home!"
  55. }
  56. })
  57. Vue.component('page1', {
  58. template: '<h1>Page1</h1><div class="content"><content/></div>',
  59. created: function () {
  60. this.msg = "Welcome to page 1!"
  61. }
  62. })
  63. Vue.component('page2', {
  64. template: '<h1>Page2</h1><div class="content"><content/></div>',
  65. created: function () {
  66. this.msg = "Welcome to page 2!"
  67. }
  68. })
  69. Vue.component('notfound', {
  70. template: '<h1>404 yo</h1>'
  71. })
  72. // simple routing
  73. var routes = ['home', 'page1', 'page2']
  74. function getRoute () {
  75. var path = location.hash.replace(/^#!\/?/, '') || 'home'
  76. return routes.indexOf(path) > -1
  77. ? path
  78. : 'notfound'
  79. }
  80. window.addEventListener('hashchange', function () {
  81. app.currentView = getRoute()
  82. })
  83. // load the app
  84. var app = new Vue({
  85. el: 'div',
  86. data: {
  87. currentView: getRoute(),
  88. routes: routes,
  89. data: {
  90. test: '123'
  91. }
  92. }
  93. })
  94. </script>