一.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" ==>唯一值 防止数据丢失