参数传参秘籍

一.axios请求 : ( 向后台请求数据)

注:传参必须都是字符串

1、get 传参

1) get id 动态传参: (手动动态id 手动+反单引)

1. let res=await this.$http.get(`/shop/goods/list?id=${id}`);  (手动)
2. let res=await this.$http.get('/shop/goods/list',{params:{id:id}})  (params方式)

2) get 普通函数 传参

1. let res=await this.$http.get('/shop/goods/list?recommendStatus=1);  (手动)
2. let res=await this.$http.get('/shop/goods/list',{params:{recommendStatus:1}}) (params方式)

3) get 多个参数 传参 ( & 连接 对象是 , 连接)

1. let res=await this.$http.get('/shop/goods/list?recommendStatus=1&name='gy'&age=18);  (手动)
2.let res=await this.$http.get('/shop/goods/list',{params:{recommendStatus:1,name:'gy'}})
(params方式)

2、 post 传参 (需要转为字符串)[ , 分割]

1.let res=await this.$http.post("/shop/goods/list","recommendStatus=1");
2. 局部qs (需在 export default 前引入 局部 import qs from 'qs')
let res=await this.$http.post('/shop/goods/list',qs.stringify({recommendStatus:1}))
3.全局qs  1>在main.js里引入全局qs  
         2> 二次封装axios
         axios.interceptors.request.use(function(config){
         console.log("config",config)
         if(config.method.toLowerCase()=='post'){
         config.data=qs.stringify(config.data)
         }
         return config;
         },function(error){
         return Promise.reject(error)
         })
let res=await this.$http.post('/shop/goods/list',{recommendStatus:1});
post id动态传参反单引号
1.let res=await this.$http.post("/shop/goods/list",`id=${id}`);
​
post 多个参数传参
1.let res=await this.$http.post("/shop/goods/list","recommendStatus=1&name='gy'&age=18");

二.路由跳转 传参

1) $router: push==>添加历史记录 Repalce ==> 替换历史记录

2) $route: 获取参数 name params query path

没有参数

this.$router.push('/home')
this.$router.push({name:"home"})
this.$router.push({path:"/home"})

有参数

this.$router.push({path/name:/要跳转的路由,query:{id:id}})
this.$router.push('地址'+id)
this.$router.push({name:要跳转的路由,params:id}) //丢失数据  ==>占位符
占位符: path:"/home/:id" ==>唯一值 防止数据丢失 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值