vue路由传参

迷南。 2022-11-29 13:28 445阅读 0赞

vue路由跳转页面传参

1、使用路由url携带参数传递

  1. 主页Main.vue模板脚本
  2. <template>
  3. <div class="hello">
  4. <h1>{
  5. {msg}}</h1>
  6. <h2>Essential Links</h2>
  7. <div @click="toSlotComo()">Go to SlotTest</div>
  8. </div>
  9. </template>
  10. <script>
  11. export default {
  12. name: 'Main',
  13. data() {
  14. return {
  15. msg: 'Welcome to Your Vue.js App'
  16. }
  17. },
  18. methods: {
  19. toSlotComo() {
  20. this.$router.push({
  21. //把【测试slot插槽】作为参数传递
  22. path: '/soltTest/测试slot插槽'
  23. })
  24. }
  25. }
  26. }
  27. </script>

路由页面SlotTest.vue

  1. <template>
  2. <div>
  3. <div class="top">{
  4. {content}}</div>
  5. <my-com>
  6. <p>月落乌啼霜满天,江枫渔火对愁眠</p>
  7. </my-com>
  8. <my-com>
  9. </my-com>
  10. //获取路由传递的参数
  11. <div>{
  12. {this.$route.params.myParam}}</div>
  13. </div>
  14. </template>
  15. <script>
  16. export default {
  17. name: 'SlotTest',
  18. data() {
  19. return {
  20. content: ' -----------------------------孤篇横绝------------------------------',
  21. myParam: ''
  22. }
  23. },
  24. components: {
  25. 'my-com': {
  26. template: '<div><slot><p>姑苏城外寒山寺,夜半钟声到客船</p></slot></div>'
  27. }
  28. },
  29. }
  30. </script>

路由配置

  1. import Vue from 'vue'
  2. import Router from 'vue-router'
  3. import Main from '@/components/Main'
  4. import SlotTest from '@/components/SlotTest'
  5. Vue.use(Router)
  6. export default new Router({
  7. routes: [
  8. {
  9. path: '/',
  10. name: 'Main',
  11. component: Main
  12. },
  13. {
  14. //myParam作为传递参数的key,再参数获取页面使用myParam获取参数
  15. path: '/soltTest/:myParam',
  16. name: 'SlotTest',
  17. component: SlotTest
  18. },
  19. ]
  20. })

使用url传参再浏览器页面可以看到传递的参数

2、通过params传递对象

主页Main.vue

  1. <template>
  2. <div class="hello">
  3. <h1>{
  4. {msg}}</h1>
  5. <h2>Essential Links</h2>
  6. <!-- <router-link to="/soltTest/测试slot插槽">Go to SlotTest</router-link> -->
  7. <div @click="toSlotComo()">Go to SlotTest</div>
  8. <div @click="toRouteTest()">Go to RouteTest</div>
  9. </div>
  10. </template>
  11. <script>
  12. export default {
  13. name: 'Main',
  14. data() {
  15. return {
  16. msg: 'Welcome to Your Vue.js App'
  17. }
  18. },
  19. methods: {
  20. toRouteTest() {
  21. this.$router.push({
  22. //生命模板名称
  23. name:'MyRouteTest',
  24. //路由传递参数传递一个对象
  25. params:{
  26. name: 'liuping',
  27. age:'31'
  28. }
  29. })
  30. }
  31. }
  32. }
  33. </script>

路由页面RouteTest.vue

  1. <template>
  2. <div>
  3. 测试路由传参RouteTest
  4. <div>
  5. //获取路由传递对象的属性
  6. <span>{
  7. {this.$route.params.name}} </span>
  8. <span>{
  9. {this.$route.params.age}} </span>
  10. </div>
  11. </div>
  12. </template>
  13. <script>
  14. export default {
  15. name:'MyRouteTest',
  16. }
  17. </script>

路由配置

  1. import Vue from 'vue'
  2. import Router from 'vue-router'
  3. import Main from '@/components/Main'
  4. import MyRouteTest from '@/components/RouteTest'
  5. Vue.use(Router)
  6. export default new Router({
  7. routes: [
  8. {
  9. path: '/',
  10. name: 'Main',
  11. component: Main
  12. },
  13. {
  14. //直接配置路由地址即可
  15. path: '/routeTest',
  16. name: 'MyRouteTest',
  17. component: MyRouteTest
  18. }
  19. ]
  20. })

发表评论

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

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

相关阅读

    相关 vue

    前言 之前,查了其他博客,总结一篇,发现貌似不对??,因此又查了一遍好不容易写了出来。 1.query传参 一种是,这样滴。 // 跳转 thi

    相关 vue-03--

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

    相关 Vue跳转+

    之前在原生JS的开发中,我们经常会用到根据某一状态进行页面的跳转。 比如:登录成功跳到首页,点击商品列表的某个商品跳转商品详情等。 而常见的写法就是: locat