功能:在A页面点击评论图标,跳转页面,并且页面上滑到页面评论部分.
解决方法:在图标那绑函数,跳转页面传参数,当值为true时,利用<ScrollView>的scrollIntoView属性,绑定scrollIntoView的值为评论的id.
代码如下:
//跳转页面的函数,当scrollToComment为true时是用户点击评论图标跳转的页面,当为false时,是点击内容进入详情页.
function toTopic(topicId, scrollToComment = false) {
let url = `/ext-pages/topic/index?topicId=${topicId}`;
if (scrollToComment) {
url += '&scrollToComment=true';
}
return Taro.navigateTo({
url,
})
}
//详情页
//为什么setTimeout:页面加载的时候在把先加载componentDidShow,那时候是拿不到参数的.
componentDidShow() {
const params = this.$router.params;
if (params.scrollToComment === 'true' || params.scrollToComment === true) {
setTimeout(() => {
this.setState({
scrollIntoView: 'commentArea',
})
}, 1000);
}
}
顺便记一下scrollView很有意思的几个属性
onScrollToLower这个属性可以用来写上滑加载更多功能,直接绑定函数就好啦,onScrollToLower={this.xxxxx.bind(this)}
注意:用这个属性的时候,还需要一个属性upperThreshold,不然没有效果的哦