vue params、query传参使用详解

最近在学习Vue,本文介绍了vue params、query传参使用,分享给大家,也给自己留个笔记

声明式:

编程式:router.push(...)

这两种方式 都可以实现跳转链接,在上篇文章继续,通过A组件跳转链接到B组件并且传参数。

1、router.push使用

router/index.js

rush:js;"> export default new Router({ routes: [ { path: '/',name: 'A',component: require('../components/A') },{ path: '/B/:name/:age',name: 'B',component: require('../components/B') } ] })

上边,在路由中为B组件添加两个参数 name,age

A组件,绑定一个@click事件,跳转B组件传参 使用params

rush:xhtml;">

这时浏览器会显示 :http://localhost:8080/#/B/xy/22

在看下query 传值及地址变化

同样在 router/index.js路由文件中 不变有两个参数name,age

rush:js;"> { path: '/B/:name/:age',component: require('../components/B') }

在A组件中,之前参数传递是通过params,

rush:js;"> this.$router.push({name:'B',age:22}});

替换后,query

rush:js;"> this.$router.push({name:'B',query:{name:'xy',age:22}});

这时浏览器会发现:http://localhost:8080/#/?name=xy&age=22

通过以上两种,页面刷新后,参数还会保留的。

获取值有些不相同: params:this.$route.params.name;

query:this.$route.query.name;

------------------------ 还有种方式--------------------------------------------

使用 router-link

rush:xhtml;"> 跳转B组件

跳转后,浏览器地址为:http://localhost:8080/#/B?name=zzz&age=22

跟 this.$router.push(...) 是一样的

rush:xhtml;"> 跳转B组件
rush:js;"> { path: '/B/:name',component: require('../components/B') }

取值

rush:js;"> this.$route.params.name

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。

相关文章

可以通过min-width属性来设置el-table-column的最小宽度。以...
yarn dev,当文件变动后,会自动重启。 yanr start不会自动重...
ref 用于创建一个对值的响应式引用。这个值可以是原始值(如...
通过修改 getWK005 函数来实现这一点。这里的 query 参数就是...
<el-form-item label="入库类型" ...
API 变动 样式类名变化: 一些组件的样式类名有所变动,可能需...