一、使用冒号(:)的形式传递参数

1,路由列表的参数设置

(1)路由列表的 path 是可以带参数的,我们在路由配置文件(router/index.js)里以冒号的形式设置参数。
(2)下面样例代码中,我在跳转到欢迎页面(/hello)时,带有两个参数:id 和 userName。
import Vue from 'vue'
import Router from 'vue-router'
import index from '@/components/index' //引入首页组件
import hello from '@/components/hello' //引入欢迎页组件 //Vue全局使用Router
Vue.use(Router) export default new Router({
routes: [ //配置路由,使用数组形式
{
path: '/', //链接路径
name: 'index', //路由名称
component: index //映射的组件
},
{
path: '/hello/:id/:userName',
name: 'hello',
component: hello
}
]
})

2,参数的传递

(1)如果使用 <router-link> 组件跳转的话,可以这么携带参数:
<router-link to="/hello/123/hangge">跳转到 hello</router-link>
(2)如果使用 js 代码跳转的话,可以这么携带参数:
this.$router.push("/hello/123/hangge");

3,参数的获取

页面中通过 $route.params.xxx 获取传递过来的数据。
<template>
<div>
<h1>ID:{{ $route.params.id}}</h1>
<h1>用户名:{{ $route.params.userName}}</h1>
</div>
</template>

4,运行效果

可以看到点击首页链接进行跳转后,参数成功传递并显示。

     
 

二、使用 query 方式传递参数

1,路由列表

query 方式类似 get 传参,即通过 URL 传递参数。而路由列表的 path 不需要配置参数:

import Vue from 'vue'
import Router from 'vue-router'
import index from '@/components/index'  //引入首页组件
import hello from '@/components/hello'  //引入欢迎页组件
 
//Vue全局使用Router
Vue.use(Router)
 
export default new Router({
  routes: [ //配置路由,使用数组形式
    {
      path: '/',   //链接路径
      name: 'index',  //路由名称
      component: index//映射的组件
    },
    {
      path: '/hello',
      name: 'hello',
      component: hello
    }
  ]
})

2,参数的传递

(1)如果使用 <router-link> 组件跳转的话,可以这么携带参数:

<router-link :to="{path:'/hello', query:{id:123, userName:'hangge'}}">
跳转到 hello
</router-link>

(2)如果使用 js 代码跳转的话,可以这么携带参数:

this.$router.push({
path:'/hello',
query:{id:123, userName:'hangge'}
});

3,参数的获取

页面中通过 $route.query.xxx 获取传递过来的数据。
<template>
<div>
<h1>ID:{{ $route.query.id}}</h1>
<h1>用户名:{{ $route.query.userName}}</h1>
</div>
</template>

4,运行效果

可以看到点击首页链接进行跳转后,参数是自动拼接到 url 后面进行传递的。

三、使用 params 方式传递参数

1,路由列表

params 方式类似于 post 传参,即传递的参数不会显示在 URL 上。同上面的 query 方式一样,路由列表的 path 不需要配置参数:
import Vue from 'vue'
import Router from 'vue-router'
import index from '@/components/index' //引入首页组件
import hello from '@/components/hello' //引入欢迎页组件 //Vue全局使用Router
Vue.use(Router) export default new Router({
routes: [ //配置路由,使用数组形式
{
path: '/', //链接路径
name: 'index', //路由名称
component: index//映射的组件
},
{
path: '/hello',
name: 'hello',
component: hello
}
]
})

2,参数的传递

注意:params 只能用 name 来引入路由,而不能用 path。

(1)如果使用 <router-link> 组件跳转的话,可以这么携带参数:

<router-link :to="{name:'hello', params:{id:123, userName:'hangge'}}">
跳转到 hello
</router-link>

(2)如果使用 js 代码跳转的话,可以这么携带参数:

this.$router.push({
name:'hello',
params:{id:123, userName:'hangge'}
});

3,参数的获取

页面中通过 $route.params.xxx 获取传递过来的数据。
<template>
<div>
<h1>ID:{{ $route.params.id}}</h1>
<h1>用户名:{{ $route.params.userName}}</h1>
</div>
</template>

4,运行效果

可以看到这种方式,参数的传递不会拼接到 url 后面。
 

附:使用 props 实现参数解耦

    从上面的样例可以看出,当路由携带参数跳转时,页面这边通过 $route.params.xxx 或 $route.query.xxx 来获取传递过来的数据。但这样有个问题,由于组件中使用 $route 会使之与其对应路由形成高度耦合,从而使组件只能在某些特定的 URL 上使用,限制了其灵活性。
    要解决这个问题,我们可以使用 props 将组件和路由解耦。props 共有如下三种模式。
 

1,布尔模式

(1)直接将路由配置中的 props 属性被设置为 true,那么参数将会被设置为组件属性。
export default new Router({
routes: [ //配置路由,使用数组形式
{
path: '/', //链接路径
name: 'index', //路由名称
component: index //映射的组件
},
{
path: '/hello/:id/:userName',
name: 'hello',
component: hello,
props: true
}
]
})

(2)然后我们页面组件这边不再需要通过 $route.params.xxx 或 $route.query.xxx 来获取传递过来的数据。

<template>
<div>
<h1>ID:{{ id }}</h1>
<h1>用户名:{{ userName}}</h1>
</div>
</template> <script>
export default {
name: 'hello',
props: ['id', 'userName']
}
</script>

2,对象模式

(1)我们可以将 props 设置为一个对象,对象内容会被设置为组件属性。这种方式常用来配置静态参数。
export default new Router({
routes: [ //配置路由,使用数组形式
{
path: '/', //链接路径
name: 'index', //路由名称
component: index //映射的组件
},
{
path: '/hello',
name: 'hello',
component: hello,
props: {
id: 1234,
userName: "hangge"
}
}
]
})

(2)然后页面组件这边获取数据方式和前面一样。

<template>
<div>
<h1>ID:{{ id }}</h1>
<h1>用户名:{{ userName}}</h1>
</div>
</template> <script>
export default {
name: 'hello',
props: ['id', 'userName']
}
</script>

3,函数模式

(1)我们还可以创建一个函数返回 props,在函数中对参数值进行处理,或者将静态值与基于路由的值结合。
function dynamicPropsFunc (route) {
return {
message: "欢迎您:" + route.params.userName
}
} export default new Router({
routes: [ //配置路由,使用数组形式
{
path: '/', //链接路径
name: 'index', //路由名称
component: index //映射的组件
},
{
path: '/hello',
name: 'hello',
component: hello,
props: dynamicPropsFunc
}
]
})

(2)这里假设我们使用 JS 进行跳转,代码如下:

this.$router.push({
name:'hello',
params:{id:123, userName:'hangge'}
});

(3)目标页面组件代码,以及运行结果如下:

<template>
<div>
<h1>{{ message }}</h1>
</div>
</template> <script>
export default {
name: 'hello',
props: ['message']
}
</script>

Vue.js - 路由 vue-router 的使用详解2(参数传递)的更多相关文章

  1. vue.js基础知识篇(6):组件详解

    第11章:组件详解 组件是Vue.js最推崇也最强大的功能之一,核心目标是可重用性. 我们把组件代码按照template.style.script的拆分方式,放置到对应的.vue文件中. 1.注册 V ...

  2. vue.js基础知识篇(2):指令详解

    第三章:指令 1.语法 指令以v-打头,它的值限定为绑定表达式,它负责的是按照表达式的值应用某些行为到DOM上. 内部指令有v-show,v-else,v-model,v-repeat,v-for,v ...

  3. Vue.js学习笔记之修饰符详解

    本篇将简单介绍常用的修饰符. 在上一篇中,介绍了 v-model 和 v-on 简单用法.除了常规用法,这些指令也支持特殊方式绑定方法,以修饰符的方式实现.通常都是在指令后面用小数点“.”连接修饰符名 ...

  4. 使用vue.js路由踩到的一个坑Unknown custom element

    在配合require.js使用vue路由的时候,遇到了路由组件报错: “vue.js:597 [Vue warn]: Unknown custom element: <router-link&g ...

  5. [转]Vue项目全局配置微信分享思路详解

    这篇文章给大家介绍了vue项目全局配置微信分享思路讲解,使用vue作为框架,使用vux作为ui组件库,具体内容详情大家跟随脚本之家小编一起学习吧 这个项目为移动端项目,主要用于接入公众号服务.项目采用 ...

  6. js 引入Vue.js实现vue效果

    拆分组件为单个js见:https://www.jianshu.com/p/2f0335818ceb 效果 html <!DOCTYPE html> <html> <hea ...

  7. Vue实例初始化的选项配置对象详解

    Vue实例初始化的选项配置对象详解 1. Vue实例的的data对象 介绍 Vue的实例的数据对象data 我们已经用了很多了,数据绑定离不开data里面的数据.也是Vue的核心属性. 它是Vue绑定 ...

  8. Vue.js起手式+Vue小作品实战

    本文是小羊根据Vue.js文档进行解读的第一篇文章,主要内容涵盖Vue.js的基础部分的知识的,文章顺序基本按照官方文档的顺序,每个知识点现附上代码,然后根据代码给予个人的一些理解,最后还放上在线编辑 ...

  9. 最详尽的 JS 原型与原型链终极详解,没有「可能是」。(一)

    最详尽的 JS 原型与原型链终极详解,没有「可能是」.(一) 第二篇已更新,点击进入第三篇已更新,点击进入

  10. JS中的函数节流throttle详解和优化

    JS中的函数节流throttle详解和优化在前端开发中,有时会为页面绑定resize事件,或者为一个页面元素绑定拖拽事件(mousemove),这种事件有一个特点,在一个正常的操作中,有可能在一个短的 ...

随机推荐

  1. vue组件实例的生命周期

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 深入理解Magento – 第二章 – Magento请求分发与控制器

    深入理解Magento 作者:Alan Storm 翻译:Hailong Zhang 第二章 – Magento请求分发与控制器 Model-View-Controller (MVC) ,模型-视图- ...

  3. 云栖PPT下载 | 开源界大咖集体现身,开源数据库专场重点再回眸!

    阿里云开源数据库项目最新发布 阿里巴巴集团副总裁.阿里云智能数据库事业部总裁.高级研究员李飞飞(飞刀).阿里云数据库资深技术专家楼方鑫(黄忠)以及阿里云数据库技术专家傅宇(齐木)三位阿里云技术专家为大 ...

  4. MySql查询结果按照指定顺序排序

    Mysql这功能做的很好用啊!! 让查询结果按照指定顺序排序 --------------------------------------------------------------------- ...

  5. 1245. Tree Diameter

    解题思路:本题是一道图的题目,但是无向图,给定的输入是图的各个边,题目中给出一个关键信息(Each node has labels in the set {0, 1, ..., edges.lengt ...

  6. 高级运维(三):部署Lnmp环境、构建Lnmp平台、地址重写

    一.部署LNMP环境 目标: 安装部署Nginx.MariaDB.PHP环境 1> 安装部署Nginx.MariaDB.PHP.PHP-FPM: 2> 启动Nginx.MariaDB.FP ...

  7. python实现收邮件判断模块poplib,email

    一.代码 # 输入邮件地址, 口令和POP3服务器地址: import datetime import email import poplib import email.policy from ema ...

  8. mysql注入篇

    博客这个东西真的很考验耐心,每写一篇笔记,都是在艰难的决定中施行的,毕竟谁都有懒惰的一面,就像这个,mysql注入篇,拖拖拖一直拖到现在才开始总结,因为这个实在是太多太杂了,细细的总结一篇太烧脑. 由 ...

  9. SDNU 1217 CD收藏——并查集

    Description     lmh平常爱听歌,所以买了很多的CD来收藏,但是因为平常整理不当,所以忘记了这些CD的歌手是谁.现在他想知道他到底收藏了多少位歌手的专辑,于是他想了一个办法,同时拿出两 ...

  10. 基于V8的JsonMapper

    <dependency> <groupId>com.eclipsesource.j2v8</groupId> <artifactId>j2v8_win3 ...