1.小程序数据同步
this.setData({
属性名:值 或
'对象.属性名':值
})
var str
= `noticeListList[${index}].readStatus`
this.setData({
[str
]: "1"
})
2.跳转到非tabBar页面
wx
.navigateTo({
url
: '/pages/customerDetails/customerDetails',
})
3.图片
image:添加mode="widthFix" width给定死
4.获取参数
<view
class="line {{item.readStatus=='0'? 'unread':''}}" bindtap
="detailnoticeList" data
-announceForm
="{{item.announceForm}}">
函数:
detailnoticeList(even
){
var announceForm
= even
.currentTarget
.dataset
.announceform
console
.log(announceForm
)
wx
.navigateTo({
url
: `/pages/noticeDetailImg/noticeDetailImg?announceForm=${announceForm}`,
})
},
5.文字一行显示
overflow:hidden
;
text-overflow:ellipsis
;
white-space:nowrap
;
width:88%
;
6.页面可进行分享或不允许分享
允许分享:
如果想不允许右上角转发分享当前页面,又想在页面里点击按钮分享并且自定义分享的 title 和 imgUrl 呢
可以在onLoad里写
this.onShareAppMessage = (e
)=>{
return {
title
:'xxxxxxxxxxxx',
path
:'/pages/xxxx/xxxx',
imgUrl
:'xxxxxxxxxxxxxxxxxxx'
}
}
在页面wxml里写
<button open
-type
="share">分享button
> 即可
不允许分享:
onShareAppMessage
方法删除即可!
7.获取数组中对应id的对象或索引值
var index
= this.data
.noticeListList
.findIndex(item
=> item
.announceId
== even
.currentTarget
.dataset
.announceid
)
var index
= this.data
.noticeListList
.find(item
=> item
.announceId
== even
.currentTarget
.dataset
.announceid
)
8.获取用户信息
wx
.getUserInfo({
success
: function (res
) {
_this
.setData({
avatarUrl
: res
.userInfo
.avatarUrl
,
nickName
: res
.userInfo
.nickName
,
})
}
})
9.修改上个页面数据
法
1:使用page栈
A页面:
changeData(value
){
this.setData({
'unread.announce':value
})
},
var pages
= getCurrentPages();
console
.log(pages
)
if (pages
.length
> 1) {
var prePage
= pages
[pages
.length
- 2];
prePage
.changeData('1'放参数
)
}
10.提示语
wx
.showToast({
title
: '这里面可以写很多的文字,比其他的弹窗都要多!',
icon
: 'none',
duration
: 2000
})
11.为对象添加属性
Obj.assign(目标对象,源对象)
//例一:原对象会变
var obj={a:1}
var obj1={b:2}
var obj3=Obj.assign(obj,obj1)
console.log(obj)-------{a:1,b:2}
//例二:原对象不改变
var var obj={a:1}
var obj1={b:2}
var obj3=obj.assign({},obj,obj1)
console.log(obj)----{a;1}
12.合并快捷键
ctrl +shift +p 调出命令窗口,输入json lines则可以实现合并多行!
13.promise理解
function getNumber(){
return new Promise(function(resolve
, reject
){
setTimeout(function(){
console
.log('执行完成');
resolve('随便什么数据');
}, 2000);
});
}
getNumber()
.then(
function(data
){
console
.log('resolved');
console
.log(data
);
},
function(reason
, data
){
console
.log('rejected');
console
.log(reason
);
}
);
getNumber()
.then(function(data
){
console
.log('resolved');
console
.log(data
);
})
.catch(function(reason
){
console
.log('rejected');
console
.log(reason
);
});
Promise
.all([getNumber(), getNumber1(), getNumber2()])
.then(function(results
){
console
.log(results
);
});
14.小程序wxs脚本
<wxs module
="wxs_util">
module
.exports
= {
fractionFormat
: function(fraction
){
return fraction
>=80 ? "优秀" : "一般"
}
}
</wxs
>
使用:
{{wxs_util
.fractionFormat(参数
)}}
<wxs src
="../tools.wxs" module
="wxs_util"></wxs
>
15.flex布局总结
//父元素样式设置
display;flex
;//开启flex布局
flex-direction: row(默认
) | row-reverse | column | column-reverse //控制主轴排列方向
flex-wrap: nowrap(默认
) | wrap | wrap-reverse //控制主轴是否换行
flex-flow: row nowrap //是flex-deriction与flex-wrap属性的简写集合
justify-content: flex-start(默认
) | flex-end | center | space-between | space-around | space-evenly //控制项目在横轴的排列方式
align-items:flex-start | flex-end | center | baseline |
stretch(默认
) //控制纵轴元素的排列方式
//项目样式设置
order:0 //越小越靠前
flex-grow:0 //决定剩余空间是否放大
flex-shrink:1 //默认空间不够时,等比例缩小
flex-basis:auto //设置项目的宽度
flex:0 1 auto //是flex-grow,flex-shrink与flex-basis三个属性的简写
16.component
js文件:
Component({
options
: {
multipleSlots
: true
},
properties
: {
coverUrl
: {
type
: String
,
value
: "http://img.youpenglai.cn/meetingpic/0b24376c43b1c372076aa65253b2f0ca123.jpg"
},
},
data
:{
isShowOrganizer
:false,
},
methods
: {
activityDetailTap
: function(e
) {
var currentPosition
= e
.currentTarget
.dataset
.currentPosition
this.triggerEvent('signEvent', {
'currentPosition': currentPosition
})
}
}
attached
: function(){},
moved
: function(){},
detached
: function(){},
})
在父组件的json页面:
"usingComponents":{
"home-item":"../components/home-item/home-item"
}
可在父组件的wxml文件里进行引入
<home
-item
></home
-item
>
17.template
// 1.在根目录下建立templates
// 2.建立wxml,wxss文件
template.wxml:
<template name="demo">
<view class='tempDemo'>
<text class='name'>FirstName: {{firstName
}}, LastName: {{lastName
}}</text>
<text class='fr' bindtap="clickMe" data-name="{{'Hello! I am '+firstName+
' '+LastName+
'!'}}"> clcikMe </text>
</view>
</template>
// 3.页面引用:
wxml引用:
<import src="模板wxml路径" />
<template is="name名称" data="{{对象形式的数据
}}"></template>
wxss引用: @import
"路径"
18.分包
├── app
.js
├── app
.json
├── app
.wxss
├── packageA
│ └── pages
│ ├── cat
│ └── dog
├── packageB
│ └── pages
│ ├── apple
│ └── banana
├── pages
│ ├── index
│ └── logs
└── utils
"subpackages": [
{
"root": "packageA",
"pages": [
"pages/cat",
"pages/dog"
]
}, {
"root": "packageB",
"name": "pack2",
"pages": [
"pages/apple",
"pages/banana"
]
}
]
19.小程序值传递
<my
-prop money
="{{money}}"></my
-prop
>
子组件内部进行接收:
properties
: {
money
: {
type
: Number
,
value
: 0
},
money
: Number
}
子组件抛事件:
sendMoney () {
this.triggerEvent('myEvent',{
money
:this.data
.money
})
}
父组件接收:
<my
-event bind
:myEvent
="getMoney"></my
-event
>
方法里:
getMoney (e
) {
console
.log(e
.detail
.money
)
this.setData({
parentMoney
: e
.detail
.money
})
}
this.selectComponent('#my_select 放class或id选择器').data
.money
20.父子组件相互调用方法
父组件中:
在onready函数中:
this.子组件名称
= this.selectComponent("#子组件id")
在需要调用的方法中:
this.子组件名称
.子组件方法()
子组件中:
事件触发,抛事件:
this.triggerEvent("自定义事件名称",参数
)
父组件中:
<child bind
:自定义事件名称
="方法名"></child
>