vue-router query和params传参(接收参数)的区别

版权声明: https://blog.csdn.net/youth_lx/article/details/79780938

<div class="markdown_views">
<!-- flowchart 箭头图标 勿删 -->
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path></svg>
<h4 id="最近在学vue今天做项目时踩到了vue-router传参的坑query和params所以决定总结一下二者的区别">最近在学vue,今天做项目时踩到了vue-router传参的坑(query和params),所以决定总结一下二者的区别。</h4>

<h2 id="直接总结干货"><a name="t0"></a>直接总结干货!!!</h2>

<h3 id="1query方式传参和接收参数"><a name="t1"></a>1.query方式传参和接收参数</h3>

<pre class="prettyprint" name="code"><code class="hljs css has-numbering">传参:
<span class="hljs-tag">this</span>.$<span class="hljs-tag">router</span><span class="hljs-class">.push</span>(<span class="hljs-rules">{
<span class="hljs-rule"><span class="hljs-attribute">path</span>:<span class="hljs-value"><span class="hljs-string">‘/xxx‘</span>
query:{
id:id
</span></span></span>}
})

接收参数:
<span class="hljs-tag">this</span>.$<span class="hljs-tag">route</span><span class="hljs-class">.query</span><span class="hljs-class">.id</span></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li></ul></pre>

<p><code>注意:传参是this.$router,接收参数是this.$route,这里千万要看清了!!!</code></p>

<h3 id="2params方式传参和接收参数"><a name="t2"></a>2.params方式传参和接收参数</h3>

<pre class="prettyprint" name="code"><code class="hljs cs has-numbering">传参:
<span class="hljs-keyword">this</span>.$router.push({
name:<span class="hljs-string">‘xxx‘</span>
<span class="hljs-keyword">params</span>:{
id:id
}
})

接收参数:
<span class="hljs-keyword">this</span>.$route.<span class="hljs-keyword">params</span>.id</code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li></ul></pre>

<p><code>注意:params传参,push里面只能是 name:‘xxxx‘,不能是path:‘/xxx‘,因为params只能用name来引入路由,如果这里写成了path,接收参数页面会是undefined!!!</code></p>

<h3 id="另外二者还有点区别直白的来说query相当于get请求页面跳转的时候可以在地址栏看到请求参数而params相当于post请求参数不会再地址栏中显示"><a name="t3"></a>另外,二者还有点区别,直白的来说query相当于get请求,页面跳转的时候,可以在地址栏看到请求参数,而params相当于post请求,参数不会再地址栏中显示</h3>

<p><img src="https://ljzjiang.github.io/PictureLibrary/animal/20140710050947_mM3TP.thumb.700_0.jpeg" alt="加油" title=""></p>

<p><strong>vue的自学之路还得继续走,坑还会继续踩,下一个坑会是神马…</strong></p> </div>

原文地址:https://www.cnblogs.com/cost/p/9888570.html

时间: 2024-08-25 07:15:26

vue-router query和params传参(接收参数)的区别的相关文章

vue router 如何使用params query传参,以及有什么区别

写在前面: 传参是前端经常需要用的一个操作,很多场景都会需要用到上个页面的参数,本文将会详细介绍vue router 是如何进行传参的,以及一些小细节问题.有需要的朋友可以做一下参考,喜欢的可以点波赞,或者关注一下,希望可以帮到大家. 本文首发于我的个人blog:obkoro1.com Vue router如何传参 params.query是什么? params:/router1/:id ,/router1/123,/router1/789 ,这里的id叫做params query:/route

vue 路由跳转,传参

一.直接跳转 //js1.this.$router.push('/ad_new') //html 2.<router-link to="/ad_check"> <div class="top-menu-name">审核</div> </router-link> 二.跳转传参 query传参,参数会显示在url后面?id=? this.$router.push({ path: '/member', query: { id

【前端小小白的学习之路】vue学习记录④(路由传参)

通过上篇文章对路由的工作原理有了基本的了解,现在我们一起来学习路由是如何传递参数的,也就是带参数的跳转. 带参数的跳转,一般是两种方式: ①.a标签直接跳转. ②点击按钮,触发函数跳转. 在上篇文章中我们已经有两个页面(Helloworld.vue&Hello.vue),现在我准备往Hello.vue里面添加3个链接,分别对应两种情况的跳转.  第一步:在原来的Hello.vue里添加路由链接跳转的代码(见第38-44行代码),添加后的Hello.vue代码如下: 1 <template&g

Vue之路由跳转传参,插件安装与配置

路由跳转 this.$router.push('/course'); this.$router.push({name:course}); this.$router.go(-1); //后退一页 this.$router.go(1): // 前进一页 <router-link to = "/course">课程页</router-link> <router-link :to="{name:'course'}"> 课程页 </r

vue请求中 post get传参方式是不同的哦

我在学习vue,项目中post请求,get请求都用到了,我发现传参方式是不一样的. post请求的例子: checkin (){ this.$http.post('my url',{ mobilePhone:this.phone, password:this.password },{ emulateJSON: true } ).then(function(res){ this.$root.userid=res.data.userid; console.log(this.$root.userid)

router-link params传参

1.router.js配置 需要在路径后定义上要传的属性名 -->       /:属性名(query方式不需要) { path: '/CreateProgress/:name1', name: 'CreateProgress', component:CreateProgress } 2.传参 与query不同的是  params是根据路由的name跳转的   而query是根据 path跳转的 <router-link :to="{name:'CreateProgress',par

vue中组件间的传参

1.父传子 父组件准备一个数据,通过自定义属性给子组件赋值,进行传递 在子组件中通过 props 属性来接收参数 <body> <div id="app"> <son passdata="msg"></son> </div> </body> <script> Vue.component('son', { template: '<div>父组件的数据为:{{ passdat

vue.js - axios Get方法传参给 .net core webapi。

1:在vue项目中通过params属性携带数据: let _self = this; axios({ method:'get', url:'http://localhost:5000/api/StuInFors/GetEFAsync/', params:{pagesize:10,pageindex:2} }).then(function(resp){ //document.write(JSON.stringify(resp.data)); console.log(resp.status); _s

vue路由params传参

路由跳转 {name:'',params:{id:''}} 子页面路由接受参数 this.$route.params.id 原文地址:https://www.cnblogs.com/renewload/p/11271242.html