axios以及vuex api请求之携带参数的问题

Vuex api   get和post请求携带参数


get请求

api接口 单个参数

api  -> index.js

方法一:

export function reqTopSong(type){
	return request({
		url:'/top/song',
		method:'get',
		params:{
			type
		}
	})
}

 方法二:

export function reqTopSong(params){
	return request({
		url:'/top/song',
		method:'get',
		params
	})
}

store -> index.js

	async getTopSong({commit},params={}){
//当前这个getTopSong这个函数在调用获取服务器数据的时候,至少传递一个参数(空对象)
    //params形参:是当用户派发action的时候,第二个参数传递过来的,至少是一个空对象
		const res = await reqTopSong(params)
		console.log(res)
		if(res.code == 200){
			commit('TOPSONG',res.data)
		}
	},

page -> index.vue

方法一:

TopSong() {
				this.$store.dispatch('getTopSong', this.type)
				// let res = await this.$api.reqTopSong(this.type)
			},

方法二:

async TopSong() {
				await this.$store.dispatch('getTopSong', {type:this.type})
			},

api接口 多个参数

 api  -> index.js

export function reqTopAlbum(params){
	return request({
		url:'/top/album',
		method:'get',
		params
	})
}

store -> index.js

	async getTopSong({commit},params={}){
		const res = await reqTopSong(params)
		console.log(res)
		if(res.code == 200){
			commit('TOPSONG',res.data)
		}
	},

page -> index.vue

await this.$store.dispatch('getTopAlbum', {
						offset: this.offset,
						limit: this.limit,
						year: this.topAlbumYear,
						month: this.topAlbumMonth
					})

post请求

api接口 单个参数

跟get的单个参数的 方法一 一样

api接口 多个参数

 api  -> index.js

export function reqUserRegister(params){
	return request({
		url:'/user/passport/register',
		method:'post',
		data:params
	})
}

store -> index.js

  async userRegister({ commit }, user) {
    let result = await reqUserRegister(user);
    if (result.code == 200) {
      return "ok";
    }
  },

page -> index.vue

 async userRegister() {
       let res = await this.$store.dispatch("userRegister", {
            phone,
            code,
            password,
          });
  },

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值