程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

vue 笔记(十六) 参数传递及重定向

发布于2021-06-08 04:40     阅读(807)     评论(0)     点赞(8)     收藏(1)


接着上一个工程写带有参数请求的路由跳转

一、

1.主页面 Main.vue 传递参数 

  1. <el-menu-item index="1-1">
  2. <!--插入的地方-->
  3. <router-link :to="{name:'UserProfile',params:{ id:1,name:'swk'}}">个人信息</router-link>
  4. </el-menu-item>
  5. <el-menu-item index="1-2">
  6. <!--插入的地方-->
  7. <router-link to="/user/list">用户列表</router-link>
  8. </el-menu-item>

name:传递的组件名

params:传递参数

改成了:to  ,即将这一属性绑定成对象

2.路由配置 index.js 里

  1. children:[
  2. {
  3. path:'/user/profile/:id/:name',
  4. name:'UserProfile',
  5. component:Profile
  6. },
  7. {
  8. path: '/user/list',
  9. component: List
  10. }
  11. ]

添加name 与上面组件名一致

/:id    添加参数占位符

3.在组件页 Profile.vue 中

  1. <template>
  2. <div>
  3. <h1>个人信息</h1>
  4. <h1>{{$route.params.id}}</h1>
  5. <h1>{{$route.params.name}}</h1>
  6. </div>
  7. </template>
  8. <script>
  9. export default {
  10. name: "UserProfile"
  11. }
  12. </script>
  13. <style scoped>
  14. </style>

注意:template中 只能有一个元素结点

使用 $route.params.id  来获得参数id的值

4.结果:

二、解耦方式

1.路由配置 index.js 中 添加支持传递参数

  1. children:[
  2. {
  3. path:'/user/profile/:id/:name',
  4. name:'UserProfile',
  5. // 支持传递参数
  6. props:true,
  7. component:Profile
  8. },
  9. {
  10. path: '/user/list',
  11. component: List
  12. }
  13. ]

2.同样在Main.vue 中 

  1. <el-menu-item index="1-1">
  2. <!--插入的地方-->
  3. <router-link :to="{name:'UserProfile',params:{ id:1,name:'swk'}}">个人信息</router-link>
  4. </el-menu-item>
  5. <el-menu-item index="1-2">
  6. <!--插入的地方-->
  7. <router-link to="/user/list">用户列表</router-link>
  8. </el-menu-item>

3. 在 组件页 Profile.vue 中

  1. <template>
  2. <div>
  3. <h1>个人信息</h1>
  4. <h1>{{$route.params.id}}</h1>
  5. <h1>{{$route.params.name}}</h1>
  6. <h2>{{id}}</h2>
  7. <h2>{{name}}</h2>
  8. </div>
  9. </template>
  10. <script>
  11. export default {
  12. props:['id','name'],
  13. name: "UserProfile"
  14. }
  15. </script>
  16. <style scoped>
  17. </style>

4.运行结果:

 

三、重定向

下面写一个重定向到首页的goHome路由路径

1. 在路由配置 index.js 中 添加一条

  1. {
  2. path: '/goHome',
  3. redirect:'/main'
  4. }

2. 主页面 Main.vue 中 添加 

  1. <el-menu-item index="1-3">
  2. <!--插入的地方-->
  3. <router-link to="/goHome">回到首页</router-link>
  4. </el-menu-item>

3.运行结果

点击回到首页,显示

重定向成功!

 

 

原文链接:https://blog.csdn.net/Sunweikai123/article/details/117532146




所属网站分类: 技术文章 > 博客

作者:麻辣小龙虾

链接:http://www.qianduanheidong.com/blog/article/123836/6e9196c55b730aee55fb/

来源:前端黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

8 0
收藏该文
已收藏

评论内容:(最多支持255个字符)