路由传参

末蓝、 2022-06-01 00:14 427阅读 0赞
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title></title>
  6. <script type="text/javascript" src="js/vue.js"></script>
  7. <script type="text/javascript" src="js/vue-router.js"></script>
  8. </head>
  9. <body>
  10. <div id="example">
  11. <router-view></router-view>
  12. </div>
  13. <script type="text/javascript">
  14. var List=Vue.component('list-component',{
  15. data:function(){
  16. return {pList:[100,200,300]}
  17. },
  18. methods:{
  19. jump(myIdex){
  20. this.$router.push('/myDetail/'+myIdex); //发送
  21. }
  22. },
  23. template:`
  24. <ul>
  25. <li v-for="(tmp,index) in pList">
  26. <button @click="jump(index)">{
  27. {tmp}}</button>
  28. </li>
  29. </ul>
  30. `
  31. });
  32. var Detail=Vue.component('detail-component',{
  33. data:function(){
  34. return {myId:""}
  35. },
  36. created:function(){
  37. this.myId=this.$route.params.id; //接收
  38. },
  39. template:`
  40. <h1>这是详情页{
  41. {myId}}</h1>
  42. `
  43. });
  44. var NotFound=Vue.component('not-found',{
  45. template:`
  46. <h1>404 page not found</h1>
  47. `
  48. });
  49. const myRoutes=[
  50. {path:'',component:List},
  51. {path:'/myList/',component:List},
  52. {path:'/myDetail/:id',component:Detail},
  53. {path:'*',component:NotFound}
  54. ];
  55. const myRouter=new VueRouter({
  56. routes:myRoutes
  57. });
  58. new Vue({
  59. el:'#example',
  60. router:myRouter
  61. })
  62. </script>
  63. </body>
  64. </html>
  65. <!DOCTYPE html>
  66. <html>
  67. <head>
  68. <meta charset="utf-8">
  69. <title></title>
  70. <script type="text/javascript" src="js/vue.js"></script>
  71. <script type="text/javascript" src="js/vue-router.js"></script>
  72. </head>
  73. <body>
  74. <div id="example">
  75. <router-view></router-view>
  76. </div>
  77. <script type="text/javascript">
  78. var Check=Vue.component('check-component',{
  79. data:function(){
  80. return {price:300}
  81. },
  82. methods:{
  83. toPay(){
  84. this.$router.push('/pay/'+this.price);
  85. }
  86. },
  87. template:`
  88. <div>
  89. <h1>商品支付页面</h1>
  90. <button @click="toPay">去支付</button>
  91. <router-link :to="'/pay/'+price">去支付</router-link>
  92. </div>
  93. `
  94. });
  95. var Pay=Vue.component('pay-component',{
  96. data:function(){
  97. return {price:""}
  98. },
  99. created:function(){
  100. this.price=this.$route.params.price;
  101. },
  102. template:`
  103. <div>
  104. <h1>商品查看页面</h1>
  105. <h1>{
  106. {price}}</h1>
  107. <router-link to="/send">去Send</router-link>
  108. </div>
  109. `
  110. });
  111. var Send=Vue.component('send-component',{
  112. methods:{
  113. toCheck(){
  114. this.$router.push('/check');
  115. }
  116. },
  117. template:`
  118. <div>
  119. <h1>商品发货页面</h1>
  120. <button @click="toCheck">返回Check</button>
  121. </div>
  122. `
  123. });
  124. var NotFound=Vue.component('not-found',{
  125. template:`
  126. <h1>404 page not found</h1>
  127. `
  128. });
  129. /*const myRoutes=[
  130. {path:'/check',component:Check},
  131. {path:'/pay/:price',component:Pay},
  132. {path:'/send',component:Send}
  133. ];
  134. const myRouter=new VueRouter({
  135. routes:myRoutes
  136. });*/
  137. new Vue({
  138. el:'#example',
  139. //router:myRouter
  140. router:new VueRouter({
  141. routes:[
  142. {path:'',component:Check},
  143. {path:'/check',component:Check},
  144. {path:'/pay/:price',component:Pay},
  145. {path:'/send',component:Send},
  146. {path:'*',component:NotFound}
  147. ]
  148. })
  149. })
  150. </script>
  151. </body>
  152. </html>

发表评论

表情:
评论列表 (有 0 条评论,427人围观)

还没有评论,来说两句吧...

相关阅读

    相关 VueRouter

    1. 前言 本小节我们介绍 VueRouter 路由组件传参。包括 params 传参、query 传参的两种方式。路由传参的知识点非常重要,在日常开发中,我们经常会通过

    相关 vue-03--

    和平时的页面跳转一样,我们可能需要在地址栏上传递某些参数,例如,从商品列表页面,跳转的某个商品的详情页面,这个时候,如果是页面跳转的时候,我们会直接在url上跟上一个商品id,