【Vue】编程式导航-两种路由跳转方式

宁睿  金牌会员 | 2024-6-14 22:48:00 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 570|帖子 570|积分 1718

问题
点击按钮跳转如何实现?


方案
编程式导航:用JS代码来进行跳转
   以前使用js是通过location.href来跳转的,但由于vue中使用的是路由,它有专门的跳转方式
  
两种语法


  • path 路径跳转 (简易方便)
  • name 定名路由跳转 (适合 path 路径长的场景)
   这两种方式都可以被称为编程式导航
  
一、path路径跳转语法

特点:简易方便
router指的是大的路由对象
  1. //简单写法
  2. this.$router.push('路由路径')
  3. //完整写法
  4. this.$router.push({
  5.   path: '路由路径'
  6. })
复制代码

二、name定名路由跳转

特点:适合 path 路径长的场景
语法:


  • 路由规则,必须在main.js中配置name配置项
    1. { name: '路由名', path: '/path/xxx', component: XXX },
    复制代码
  • 通过name来进行跳转
    1. //简单写法
    2. this.$router.push('路由名')
    3. //完整写法
    4. this.$router.push({
    5.   name: '路由名'
    6. })
    复制代码

三、代码示例

main.js
  1. import Home from '@/views/Home'
  2. import Search from '@/views/Search'
  3. import NotFound from '@/views/NotFound'
  4. import Vue from 'vue'
  5. import VueRouter from 'vue-router'
  6. Vue.use(VueRouter) // VueRouter插件初始化
  7. // 创建了一个路由对象
  8. const router = new VueRouter({
  9.   // 注意:一旦采用了 history 模式,地址栏就没有 #,需要后台配置访问规则
  10.   mode: 'history',
  11.   routes: [
  12.     { path: '/', redirect: '/home' },
  13.     { path: '/home', component: Home },
  14.     { name: 'search', path: '/search/:words?', component: Search },
  15.     { path: '*', component: NotFound }
  16.   ]
  17. })
  18. export default router
复制代码
Home.vue
  1. <template>
  2.   <div class="home">
  3.     <div class="logo-box"></div>
  4.     <div class="search-box">
  5.       <input type="text">
  6.       <button @click="goSearch">搜索一下</button>
  7.     </div>
  8.     <div class="hot-link">
  9.       热门搜索:
  10.       <router-link to="/search/黑马程序员">黑马程序员</router-link>
  11.       <router-link to="/search/前端培训">前端培训</router-link>
  12.       <router-link to="/search/如何成为前端大牛">如何成为前端大牛</router-link>
  13.     </div>
  14.   </div>
  15. </template>
  16. <script>
  17. export default {
  18.   name: 'FindMusic',
  19.   methods: {
  20.     goSearch () {
  21.       // 1. 通过路径的方式跳转
  22.       // (1) this.$router.push('路由路径') [简写]
  23.       his.$router.push('/search')
  24.       // (2) this.$router.push({     [完整写法]
  25.       //         path: '路由路径'
  26.       //     })
  27.       this.$router.push({
  28.               path: '/search'
  29.       })
  30.       // 2. 通过命名路由的方式跳转 (需要给路由起名字) 适合长路径
  31.       //    this.$router.push({
  32.       //        name: '路由名'
  33.       //    })
  34.       this.$router.push({
  35.         name: 'search'
  36.       })
  37.     }
  38.   }
  39. }
  40. </script>
  41. <style>
  42. .logo-box {
  43.   height: 150px;
  44.   background: url('@/assets/logo.jpeg') no-repeat center;
  45. }
  46. .search-box {
  47.   display: flex;
  48.   justify-content: center;
  49. }
  50. .search-box input {
  51.   width: 400px;
  52.   height: 30px;
  53.   line-height: 30px;
  54.   border: 2px solid #c4c7ce;
  55.   border-radius: 4px 0 0 4px;
  56.   outline: none;
  57. }
  58. .search-box input:focus {
  59.   border: 2px solid #ad2a26;
  60. }
  61. .search-box button {
  62.   width: 100px;
  63.   height: 36px;
  64.   border: none;
  65.   background-color: #ad2a26;
  66.   color: #fff;
  67.   position: relative;
  68.   left: -2px;
  69.   border-radius: 0 4px 4px 0;
  70. }
  71. .hot-link {
  72.   width: 508px;
  73.   height: 60px;
  74.   line-height: 60px;
  75.   margin: 0 auto;
  76. }
  77. .hot-link a {
  78.   margin: 0 5px;
  79. }
  80. </style>
复制代码

四、path路径跳转传参

问题
点击搜索按钮,跳转须要把文本框中输入的内容传到下一个页面如何实现?

两种传参方式
1.查询参数
2.动态路由传参
传参
两种跳转方式,对于两种传参方式都支持:
① path 路径跳转传参
② name 定名路由跳转传参
path路径跳转传参(query传参)
  1. // 简单写法
  2. this.$router.push('/路径?参数名1=参数值1&参数2=参数值2')
  3. // 完整写法(更适合传参)
  4. this.$router.push({
  5.   path: '/路径',
  6.   query: {
  7.     参数名1: '参数值1',
  8.     参数名2: '参数值2'
  9.   },
  10.   replace: true // 不占用浏览器的历史记录
  11. })
复制代码
接受参数的方式依然是:$route.query.参数名

path路径跳转传参(动态路由传参)
   首先修改路由:word
  1. //简单写法
  2. this.$router.push('/路径/参数值')
  3. //完整写法
  4. this.$router.push({
  5.   path: '/路径/参数值'
  6. })
复制代码
接受参数的方式依然是:$route.params.参数值
**留意:**path不能共同params使用

五、编程式导航-name定名路由传参

name 定名路由跳转传参 (query传参)
  1. this.$router.push({
  2.   name: '路由名字',
  3.   query: {
  4.     参数名1: '参数值1',
  5.     参数名2: '参数值2'
  6.   }
  7. })
复制代码
通过this.$route.query.参数名
name 定名路由跳转传参 (动态路由传参)
  1. this.$router.push({
  2.   name: '路由名字',
  3.   params: {
  4.     参数名: '参数值',
  5.   }
  6. })
复制代码

六、总结

路径长,使用name,路径多,使用动态路由传参
1.path路径跳转


  • query传参
    1. this.$router.push('/路径?参数名1=参数值1&参数2=参数值2')
    2. this.$router.push({
    3.   path: '/路径',
    4.   query: {
    5.     参数名1: '参数值1',
    6.     参数名2: '参数值2'
    7.   }
    8. })
    复制代码
  • 动态路由传参
    1. this.$router.push('/路径/参数值')
    2. this.$router.push({
    3.   path: '/路径/参数值'
    4. })
    复制代码
2.name定名路由跳转


  • query传参
    1. this.$router.push({
    2.   name: '路由名字',
    3.   query: {
    4.     参数名1: '参数值1',
    5.     参数名2: '参数值2'
    6.   }
    7. })
    复制代码
  • 动态路由传参 (须要配动态路由)
    1. this.$router.push({
    2.   name: '路由名字',
    3.   params: {
    4.     参数名: '参数值',
    5.   }
    6. })
    复制代码

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

宁睿

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表