uniapp接口封装

uniapp 基础 https://uniapp.dcloud.net.cn/
使用hbuilderx 新建项目(uniapp + vue2.0 + vuex + js)
需要对vue有一定的掌握

uniapp项目的新建->vuex配置->配置接口
(使用hbuilderx 新建项目)

store

index.js (和vue的store写法一致)
①引入vue 引入vueX

import Vue form Vue
import Vuex from Vuex
Vue.use(Vuex)

②设置token(cookie)、用户信息、登录状态、…

export default new Vuex.Store({
state:{
token:uni.getStorageSync('token')?uni.getStorageSync('token'):'',
userInfo:''
},
mutations:{
setToken(state,token) {
state.token = token
uni.setStorageSync('token',token)
},
setUserInfo(state,userInfo) {
state.userInfo = userInfo
}
},
actions:{
	// 修改信息
}
})

③ 设置公共接口地址->新建config文件夹->新建index.js

const CONFIG = {
  serverAddress:'http://xxx.xxx.x.xxx:xxxx',
  useTokenAuthenication:true
};
CONFIG.serverAddress = "http://xxx.xxx.x.xxx:xxxx";
export default CONFIG;

④封装接口 新建http文件夹->新建index.js

// index.js中
import store from "../store/index.js"
import ENV from "../config/index.js"
// 公共接口地址
const BASE_URL = ENV.serverAddress;
export 
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值