小程序分包链接:
https://developers.weixin.qq.com/miniprogram/dev/framework/subpackages/basic.html
小程序tabbar链接:
1.自定义模板切换tabbar;引用文章:https://www.jianshu.com/p/608404463c35
小程序封装ajax:
// promise
new Promise((resolve, reject) => {
resolve() // 用于返回成功数据
reject() // 用于返回错误数据
})
const app = getApp()
const requestto = (token,url, options) => {
return new Promise((resolve, reject) => {
wx.request({
url: url,
method: options.method,
data: options.method === ‘GET’ ? options.data : JSON.stringify(options.data),
header: {
‘Content-Type’: ‘application/json; charset=UTF-8’,
‘Authorization’: 'Bearer '+token
},
success(request) {
if (request.statusCode === 200) {
resolve(request.data)
} else if (request.statusCode === 401){
app.globalData.token = “”;
wx.clearStorage()
wx.navigateTo({
url: ‘/pages/login/login’,
})
reject(request.data)
} else {
reject(request.data)
}
},
fail(error) {
console.log(error)
reject(error.data)
}
})
})
}
const request = (url, options) => {
return new Promise((resolve, reject) => {
wx.request({
url: url,
method: options.method,
data: options.method === ‘GET’ ? options.data : JSON.stringify(options.data),
header: {
‘Content-Type’: ‘application/json; charset=UTF-8’,
},
success(request) {
if (request.statusCode === 200) {
resolve(request.data)
} else{
reject(request.data)
}
},
fail(error) {
reject(error.data)
/*
switch (error.response.status){
case 401:
// this.$router.replace("/login");
localStorage.removeItem('Authorization');
location.href="/login?app_id=" + error.config.params.app_id;
break;
case 601:
location.href="/storeclose/index?app_id=" + error.config.params.app_id;
break;
} */
}
})
})
}
const requestget = (url, options) => {
return new Promise((resolve, reject) => {
wx.request({
url: url,
method: options.method,
data: JSON.stringify(options.data),
header: {
‘Content-Type’: ‘application/json; charset=UTF-8’,
},
success(request) {
if (request.statusCode === 200) {
resolve(request.data)
} else {
reject(request.data)
}
},
fail(error) {
reject(error.data)
}
})
})
}
var host = ‘https://xx.xx.xx/’;
var hostquke = ‘https://xxx.xx.xx/’;
const get = ( url, options= {}) => {
var url = host + url
return request(url, { method: ‘GET’, data: options })
}
const getto = ( token, url, options = {}) => {
var url = host + url
return requestto(token, url, { method: ‘GET’, data: options })
}
const postto = (token, url, options = {}) => {
var url = host + url
return requestto(token, url, { method: ‘POST’, data: options })
}
const post = (url, options) => {
var url = host + url
return request(url, { method: ‘POST’, data: options })
}
const getquke = (url, options = {}) => {
var url = hostquke + url
return request(url, { method: ‘GET’, data: options })
}
const postquke = (url, options) => {
var url = hostquke + url
return request(url, { method: ‘POST’, data: options })
}
const getstring = (url, options) =>{
var url = host + url
return requestget(url, { method: ‘GET’, data: options })
}
/* const put = (url, options) => {
return request(url, { method: ‘PUT’, data: options })
}
// 不能声明DELETE(关键字)
const remove = (url, options) => {
return request(url, { method: ‘DELETE’, data: options })
}
*/
/暴露出去的
module.exports = {
get,
post,
getquke,
postquke,
getto,
getstring,
postto
}
小程序自定义组件:
/*
子组件 js
properties: 接收父级参数,类似props
observers: 监听事件,当监听到data数据,就把它赋值给一个新的变量
*/
properties: {
data: {
type: String,
value: {}
}
},
data: {
// 这里是一些组件内部数据
homepage:[]
},
observers: {
‘data’: function (newVal) {
// 在 numberA 或者 numberB 被设置时,执行这个函数
this.setData({
homepage: newVal
})
}
}