支付宝
头条
百度
字节跳动
微信
序号 | 差异 | vue框架 | 微信小程序 |
---|---|---|---|
1 | 样式 | style标签 |
.wxss文件 |
2 | 行为 | script标签 |
.js文件 |
< wxs >标签 |
|||
3 | 结构 | template标签 |
.wxml文件 |
4 | 盒子 | div |
view |
5 | 文本 | span |
text |
6 | 属性绑定 |
|
|
7 | 流程分支 | v-for |
wx:for |
8 | 判断 | v-if()=''/v-else |
wx:if='{ {}}'/wx:else |
9 | 显示隐藏 | v-show='' |
hidden='{ {}}' |
10 | 生命周期 | 8种 |
应用3种+页面5种+组件6种 |
11 | 编程式路由 | v-show='' |
hidden='{ {}}' |
12 | 组件式路由 |
|
|
13 | 路由参数 |
|
|
14 | 网络请求 | axios |
wx.request() |
15 | 绑定事件 | @click |
bindtop='' |
16 | 双向绑定 | v-model='' |
bindinput='' |
17 | 获取data数据 | this.msg |
this.data.msg |
18 | 设置data数据 | this.msg='' |
this.setdata({msg:''}) |
19 | 创建方法 | methods:{test(){}} |
与data同级直接创建 |
"tabBar": {
"color": "#dddddd",
"selectedColor": "#16f2e7",
"backgroundColor": "#fff",
"borderStyle": "black",
"list": [
{
"pagePath": "pages/home/home",
"text": "主页",
"iconPath": "tabs/home.png",
"selectedIconPath": "tabs/home-active.png"
},
{
"pagePath": "pages/index/index",
"text": "消息",
"iconPath": "tabs/message.png",
"selectedIconPath": "tabs/message-active.png"
}
]
},
wx.navigateTo({url: '/pages/test/test'})
wx.redirectTo({url: '/pages/test/test'})
wx.switchTab({url: '/pages/index/index'})
wx.reLaunch({url: '/pages/index/index'})
wx:request
const baseUrl='http://jsonplaceholder.typicode.com/'
module.exports=(url,data,method)=>{
return new Promise((resolve,reject)=>{
wx.showLoading({
title: 'loading',
mask: true,
success: (res) => {
},
fail: (res) => {
},
complete: (res) => {
wx.hideLoading()
},
})
wx.request({
url: baseUrl+url,
data: data,
method,
success:resolve,
fail:reject,
complete: (res) => {
},
})
})
}
const app = getApp()
const request = (url, options) => {
// 请求之前提示加载中
wx.showLoading({
title: '加载中...'})
return new Promise((resolve, reject) => {
wx.request({
url: `${
app.globalData.baseURL}${
url}`,
// url: host + url,
method: options.method,
data: options.method === 'GET' ? options.data : JSON.stringify(options.data),
header: {
'Content-Type': 'application/json; charset=UTF-8',
'x-token': 'x-token' // 看自己是否需要
},
success: resolve,
fail: reject,
// success(request) {
// // console.log(request)
// if (request.statusCode === 200) {
// resolve(request.data)
// } else {
// reject(request.data)
// }
// },
// fail(error) {
// reject(error.data)
// }
complete() {
wx.hideLoading()
}
})
})
}
const get = (url, options = {
}) => {
return request(url, {
method: 'GET', data: options })
}
const post = (url, options) => {
return request(url, {
method: 'POST', 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,
put,
remove
}
npm i flyio -S
import Fly from '../flyio/dist/npm/wx'
const fly = new Fly()
const host = 'http://jsonplaceholder.typicode.com/'
// 添加请求拦截器
fly.interceptors.request.use(
(request) => {
wx.showLoading({
title: '加载中',
mask: true
})
console.log(request)
// request.headers["X-Tag"] = "flyio";
// request.headers['content-type']= 'application/json';
request.headers = {
'X-Tag': 'flyio',
'content-type': 'application/json'
}
let authParams = {
// 公共参数
'categoryType': 'SaleGoodsType@sim',
'streamNo': 'wxapp153570682909641893',
'reqSource': 'MALL_H5',
'appid': 'string',
'timestamp': new Date().getTime(),
'sign': 'string'
}
request.body && Object.keys(request.body).forEach((val) => {
if (request.body[val] === '') {
delete request.body[val]
};
})
request.body = {
...request.body,
...authParams
}
return request
})
// 添加响应拦截器
fly.interceptors.response.use((response) => {
wx.hideLoading()
return response.data// 请求成功之后将返回值返回
},
(err) => {
// 请求出错,根据返回状态码判断出错原因
console.log(err)
wx.hideLoading()
if (err) {
return '请求失败'
};
}
)
fly.config.baseURL = host
export default fly
源码地址