内置组件 https://hellouniapp.dcloud.net.cn/pages/component/switch/switch
组件文档 https://www.bookstack.cn/read/uniapp-component/8bbc8a6b239e1fad.md
aip文档 https://uniapp.dcloud.io/api/README
扩展组件uni-ui
https://ext.dcloud.net.cn/plugin?id=55
https://ext.dcloud.net.cn/plugin?name=uni-swipe-action
const powerNum = 3
this.message = `您有${powerNum}次机会`
<view>控制器 {{ `${aeratorRegulator.name}:${aeratorRegulator.eui}` }}</view>
var a = [11,50,40,3,5,80,90,4]
function some(item,index,array){
console.log(item);
return item>10
}
a.some(some);
//11
//true
注意:some如果遇到 true 就不在执行了。
如果都为 false 返回false。
不会改变原数组
当你发现你给对象加了一个属性,在控制台能打印出来,但是却没有更新到视图上时,也许这个时候就需要用到this. s e t ( ) 这 个 方 法 了 , 简 单 来 说 t h i s . set()这个方法了,简单来说this. set()这个方法了,简单来说this.set的功能就是解决这个问题的啦。官方解释:向响应式对象中添加一个属性,并确保这个新属性同样是响应式的,且触发视图更新。
const updateData = {
...this.tabList[index],
loadMore: 'loading',
page: this.tabList[index].page + 1
}
this.$set(this.tabList, index, updateData)
如果没有符合条件的元素返回 -1
uni.setTabBarBadge({
index: 0,
text: '1'
})
如果没有找到匹配的字符串则返回 -1。
var a = [1,2,3,'4','5','6'];
console.log(a.indexOf(3)); //2
console.log(a.indexOf('4')); //3
console.log(a.indexOf(4)); //-1
var patt = /^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/; // 保留两位小数点
let checkRes = patt.test(this.getAmount)
如果找到匹配的字符串则返回 true,否则返回 false。
var str = "Hello world,";
var n = str.includes("world"); // true
["9", "8"]
[9, 8]
this.checkList = this.List ? this.List .map(i=>+i) : []
onUnload() {}
const {firmId, firmName} = this.userDetails
123 + ‘’
onLoad: function (option) { //option为object类型,会序列化上个页面传递的参数
console.log(option.id); //打印出上个页面传递的参数。
console.log(option.name); //打印出上个页面传递的参数。
}
if (!firmId) {
return this.showToastMsg(‘商行id错误’)
}
tap和click都是点击事件。不过移动端有太多复杂的功能是click监听不到的,例如,触摸、按住和轻滑。这时候就要用tap方法了。另外,click事假是点击放开之后才触发的,所以时间上会有延迟,大概200-300ms这样,可是我们在移动端的话就比较追求速度,所以就不能出现说有延迟的情况。所以用tap来代替click事件的话,对于针对移动设备的产品都适合。而且,tap还有一个特点就是『事件穿透』,就是你执行完绑定的tap事件之后呢,如果下面如果绑定了其他事件或者是本身就存在点子事件的话,也会默认触发。
{{item.menoy | toFixedTwo}}
toFixedTwo(value) {
const newVal = parseFloat(value).toFixed(2)
return newVal
}
var array1 = [1,4,9,16];
const map1 = array1.map(x => x *2);
console.log(map1);
---------------------------------------------------------
const map1 = array1.map(x => {
if (x == 4) {
return x * 2;
}
});
console.log(map1);
---------------------------------------------------------
const map1 = array1.map(x => {
if (x == 4) {
return x * 2;
}
return x;
});
这里注意箭头函数有两种格式:
1.只包含一个表达式,这时花括号和return都省略了。
2.包含多条语句,这时花括号和return都不能省略。
imgList:{
path:[]
},
IMAGE_HOST: this.$IMAGE_HOST,
isIPx: getApp().globalData.isIPx,
active: 0, // 当前优惠tab栏
actualStatus: false,
payActive: '现金', // 当前支付方式tab栏
searchTimer: null,
loadMore: 'loading',
triggered: false
payMethod: [{
id: 1,
name: '现金',
img: this.$IMAGE_HOST + '/cash.svg'
},
{
id: 2,
name: '支付宝',
img: this.$IMAGE_HOST + '/Alipay.svg'
},
{
id: 3,
name: '微信',
img: this.$IMAGE_HOST + '/WeChat.svg'
},
{
id: 4,
name: '银联',
img: this.$IMAGE_HOST + '/UnionpayPay.svg'
},
{
id: 5,
name: '其他',
img: this.$IMAGE_HOST + '/else.svg'
},
],
tabList: [
{
id: 1,
name: "推荐",
list: [],
page: 1, // 页码
column_id: 0,
loadMore: 'loading',
triggered: true
},
{
id: 2,
name: '农讯',
list: [],
page: 1,
column_id: 1,
loadMore: 'loading',
triggered: true
},
{
id: 3,
name: '农技',
list: [],
page: 1,
column_id: 2,
loadMore: 'loading',
triggered: true
},
{
id: 4,
name: '农趣',
list: [],
page: 1,
column_id: 3,
loadMore: 'loading',
triggered: true
},
{
id: 5,
name: '热点',
list: [],
page: 1,
column_id: 4,
loadMore: 'loading',
triggered: true
}
],
this.$nextTick(() => {
// dom元素更新后执行,因此这里能正确打印更改之后的值
console.log(that.$refs.tar.innerText) // 改变了的值
})
<button class="nextBtn" :disabled="!pondFlag" :data-id="-1" @click="next">下一步</button>
next({ currentTarget: { dataset }}) {
const { id } = dataset
}
:class="['title', deviceInfo.onlineFlag === 0 ? 'gay_bg' : deviceInfo.faultFlag === 0 ? 'green_bg' : 'orange_bg']"
:class="['icons', {'close_disabled': deviceInfo.onlineFlag === 0 }]"
:class="['times', timesVal === item.value ? 'selectTimes' : '' ]"
:class="['img', { 'active': item.select }]"
:class="i.status === '异常' ? 'orange_color' : i.status === '正常' ? '' : 'white_color'"
:style
:style="{'backgroundColor':item.infoBackgroundColor?item.infoBackgroundColor:'#ff0000',
color:item.infoColor?item.infoColor:'#fff'}"
:style="{ 'height': showTop ? '0' : '' }"
this.Edit = index === -1 ? false : true
const deviceName = onlineFlag === 0 ? '失联' : faultFlag === 0 ? '' : '异常'
{{ parseFloat(userDetails.monthReceivableAmount).toLocaleString() }}
<view class="tab_item " v-for="(item, index) in tabList" @click="tab(item.id)" :key="index">
<text class="tab_item_text" :class="item.id == activeTab ? 'tab_active' : ''">{{ item.name }}({{ item.totalNum }})</text>
<text :class="item.id == activeTab ? 'tab_item_border' : ''"></text>
</view>
{{ userDetails.firmName || '我的商行' }}
{{ `${info.name}:${info.eui}` }}
<text>{{ `${info.name}:${info.eui}` }}</text>
{{inputValue.length > 500 ? 500 : inputValue.length}}/500
<textarea v-model="inputValue" maxlength="500" placeholder="请输入你的建议和问题,我们会及时处理..." />
<text class="numbers fontDIN">{{ parseInt(userDetails.orderCount || 0).toLocaleString() }}</text>
.fontDIN {
font-family: DINCondensed;
}
<view class="img" v-if="imgList.path!==''" v-for="(item,index) in imgList.path" :key="index">
<image class="image" :src="item" mode="scaleToFill"></image>
https://hellouniapp.dcloud.net.cn/pages/component/view/view
// 当前页面的数据
computed: {
fishName() {
if (!this.fishList.length) {
return '请选择'
}
return this.fishList.map(e => e.fishName).join(',')
}
},
breedFish() {
uni.navigateTo({
url: '/pagesDevice/fishPond/breedFish/index',
events: {
acceptData: data => (this.fishList = data)
// 接收被打开页 this.fishList = fishTpye
},
success: res => res.eventChannel.emit('deliver', this.fishList)
})
},
uni.navigateTo({
url: '../newFishPond/index',
events: {
acceptAquafarmId: aquafarmId => {
this.aquafarmId = aquafarmId
this.getFishInfo() // 还可以这样
}
},
success: res => res.eventChannel.emit('sendFishInfo', data)
})
// 被打开页
_updateData() {
const fishTpye = []
this.fingerlingList.filter(e => {
if (e.select) {
delete e.select
const obj = {
...e,
fishId: e.id
}
fishTpye.push(obj)
}
})
this.eventChannel.emit('acceptData', fishTpye)
},
<image :src="item.imgUrl" mode="aspectFit" @error="imgLoadFail(index)" />
index.vue
uni.navigateTo({
url: '../newFishPond/index',
events: {
acceptAquafarmId: aquafarmId => {
this.aquafarmId = aquafarmId
this.getFishInfo()
}
},
success: res => res.eventChannel.emit('sendFishInfo', data)
})
test.vue
onLoad() {
const eventChannel = this.getOpenerEventChannel()
this.eventChannel = eventChannel
eventChannel.on('sendFishInfo', data => {
const { isEdit, id, objectVersionNumber, controllCount } = data
this.isEdit = isEdit
this.controllCount = controllCount
if (isEdit) {
uni.setNavigationBarTitle({
title: '编辑鱼塘'
})
}
this.aquafarmId = id
this.objectVersionNumber = objectVersionNumber
this.getFishInfo()
})
},
this.eventChannel.emit('acceptAquafarmId', this.aquafarmId)
userHelp () {
uni.showLoading({
title: '下载中...',
mask: true
})
uni.downloadFile({
url: '', // 请求链接
success: (res) => {
console.log(res)
if (res.statusCode === 200) {
uni.hideLoading()
uni.saveFile({
tempFilePath: res.tempFilePath,
success:(resp)=> {
console.log(resp.savedFilePath);
uni.showToast({
icon: 'none',
mask: true,
title: '文件已保存:' + resp.savedFilePath, //保存路径
duration: 3000,
});
setTimeout(() => {
//打开文档查看
uni.openDocument({
filePath: resp.savedFilePath,
success: function(respo) {
console.log('打开文档成功');
}
});
},3000)
}
});
}
},
fail: (err) => {
uni.hideLoading()
uni.showToast({
icon: 'none',
mask: true,
title: '失败请重新确认',
});
},
});
}
uni.setTabBarBadge({
index: 0,
text: '1'
})
uni.setNavigationBarTitle({title:‘设置标题(可以是变量)’})
1、标签属性值
switchUserSetting(event) {
const { itemId } = event.target.dataset
}
<checkbox :checked="seletedAll" @click="seletedAllCheck"/>全选
<checkbox-group @change="checkboxChange"> 多选
<checkbox :value="item.orderId" :checked="checkList.includes(item.orderId)" />
</checkbox-group>
// checkbox 单选
checkboxChange(e) {
this.checkList = e.detail.value;
this.checkList = e.detail.value ? e.detail.value.map(i=>+i) : []
if (this.checkList.length == this.orderList.length){
this.seletedAll = true;
} else {
this.seletedAll = false;
}
this.getCalculation(this.checkList)
},
// checkbox 全选
seletedAllCheck(e) {
this.checkList=[]
this.totalAmount = 0
this.totalNum = 0
if (!this.seletedAll) {
this.seletedAll=true
this.orderList.map(item=>{
this.checkList.push(item.orderId)
})
} else {
this.seletedAll=false
}
this.getCalculation(this.checkList)
},
getUserDetails() {
this.$api.getUserDetails().then(resp => {
const data = resp.data
if (resp.statusCode === 200) {
if (data.resCode === 200) {
this.userDetails = data.body
let resetStorageFlag = false
uni.setStorageSync('userSetting', data.body.userSetting)
if (resetStorageFlag) {
uni.setStorageSync('user', this.storageUser)
}
}
}
}).catch(console.log)
},
获取上页面的缓存值
this.areaList = uni.getStorageSync(‘areaList’)
// 传参
clickBusinessInfo() {
const { firmId, firmName, firmAddress, firmContactText } = this.userDetails
uni.navigateTo({
url: '../businessInfo/index',
success: res => {
res.eventChannel.emit('transData', { firmId, firmName, firmAddress, firmContactText })
}
})
}
// 接收
onLoad() {
const eventChannel = this.getOpenerEventChannel()
eventChannel.on('transData', ({ firmId, firmName, firmAddress, firmContactText }) => {
this.firmId = firmId
this.firmName = firmName
this.address = firmAddress
this.contactText = firmContactText
})
},
uni.navigateTo({
url: `../aa/index?id=${this.aquafarmId}&code=${this.code}`
})
onLoad({ id, code}) {
this.arr = { id: +id, code: +code}
},
多个传参
let data = { name, eui, id, aquafarmId, aquafarmName }
data = JSON.stringify(data)
uni.navigateTo({
url: `/pages/my/detail/index?data=${data}`
})
接参
onLoad({ data }) {
this.fishPondInfo = JSON.parse(data)
this.getAllController()
},
/**
* 提示
*/
showToastMsg(title, icon) {
uni.showToast({
title: title,
icon: icon || 'none',
duration: 3000
})
},
uni.switchTab({
url: '/pages/index/index'
});
share(){
uni.navigateTo({
url:'../debtList/index'
})
},
uni.showLoading({
title: '保存中...',
mask: true
})
uni.hideLoading()
uni.showModal({
title: '后台打印地址',
content: printUrl,
confirmText: '复制',
success: res => {
if (res.confirm) {
uni.setClipboardData({
data: printUrl
})
}
}
})
uni.setNavigationBarTitle({
title: '修改员工'
})
.container {
display: flex;
flex-direction: column;
flex-wrap: nowrap;
height: 100%;
box-sizing: border-box;
}
.footer {
background: white;
display: flex;
flex-direction: column;
padding: 0 30rpx;
padding-bottom: constant(safe-area-inset-bottom);
padding-bottom: env(safe-area-inset-bottom);
}
:class="isIPx ? 'fix-iphonex' : ''"
.fix-iphonex {
padding-bottom: 68rpx !important;
}
<view class="deleIcon"></view>
<text class="icon" @click="deleteImg(index,item)">ⅹ</text>
.deleIcon{
position: absolute;
top: 0;
right: 0;
width: 0;
height: 0;
border-top: 60rpx solid #ff5555;
border-left: 60rpx solid transparent;
}
.icon{
position: absolute;
top: -4rpx;
right: 4rpx;
color: #f7f7f7;
font-size: 26rpx;
}
.table_ananlysis {
display: table;
border-collapse: collapse;
width: 610rpx;
.table_tr {
display: table-row;
font-size: 24rpx;
text-align: center;
.table_th {
display: table-cell;
border: 2rpx solid #f1f1f1;
padding: 28rpx 0;
color: #333333;
background-color: #f8f8f8;
}
.table_td {
display: table-cell;
border: 2rpx solid #f1f1f1;
padding: 28rpx 0;
color: #666666;
}
}
}
<textarea v-model="contactText" auto-height maxlength="255" :disabled="feedbackMsg.status == '录入'?false:true" />
<input class="stillowing" :value="firmName" focus @input="inputFirmName" placeholder-style="color:#bbb">
inputFirmName({ detail }) {
this.firmName = detail.value
},
inputFirmName: function(event) {
this.inputValue = event.target.value
},
<input
type="number"
v-model="staffUser.phoneNum"
@focus="clickFocus(2)"
:class="status === 2 ? 'focusstyle' : ''"
maxlength="11"
placeholder="请输入员工电话,此号码将用于员工登录"
placeholder-style="color:#bbbbbb;font-size:30rpx"
/>
clickFocus(status) {
this.status = status
},
<button hover-class='none' type="default">新增</button>
https://ext.dcloud.net.cn/plugin?id=5603
https://ext.dcloud.net.cn/plugin?id=7027