UniApp提供了丰富的组件和API,用于构建功能完善的应用。
// 网络请求
uni.request({
url: 'https://api.example.com/data',
method: 'GET',
success: (res) => {
console.log('请求成功:', res.data);
this.dataList = res.data;
},
fail: (err) => {
console.log('请求失败:', err);
uni.showToast({
title: '网络请求失败',
icon: 'none'
});
}
});
// 本地存储
uni.setStorage({
key: 'userInfo',
data: {
name: '张三',
age: 25
},
success: () => {
uni.showToast({
title: '保存成功'
});
}
});
// 获取本地存储
uni.getStorage({
key: 'userInfo',
success: (res) => {
this.userInfo = res.data;
}
});
提示: 这是一个重要的概念,需要特别注意理解和掌握。
注意: 这是一个常见的错误点,请避免犯同样的错误。