reactjs - Ionic IonRefresher 渲染 TypeError 不是函数
问题描述
这是我的代码:
class Calendar extends React.Component<{},State> {
componentDidMount() {
this.fetchCalendarApi(); //>> Working
}
fetchCalendarApi(){
//Doing my API call here
}
doRefresh(event: CustomEvent<RefresherEventDetail>){
console.log('Begin async operation');
setTimeout(() => {
console.log('Async operation has ended');
this.fetchCalendarApi(); //>> Not working
event.detail.complete();
}, 2000);
}
renderCal(){
return (
<IonPage>
<IonContent>
<IonRefresher slot="fixed" onIonRefresh={this.doRefresh}>
<IonRefresherContent></IonRefresherContent>
</IonRefresher>
</IonContent>
</IonPage>
)
}
}
当我实际使用 pull refresh 时出现此错误:
TypeError:this.fetchCalendarApi 不是 [...] 的函数
谢谢
解决方案
您需要将 doRefresh 函数更改为箭头函数:
doRefresh = (event: CustomEvent<RefresherEventDetail>) => {
console.log('Begin async operation');
setTimeout(() => {
console.log('Async operation has ended');
this.fetchCalendarApi(); //>> Not working
event.detail.complete();
}, 2000);
}
问题是因为您使用的是普通函数,因此无法获取 fetchCalendarApi() 函数,因为它不在范围内。通过使用箭头函数,this 将引用定义方法的类。
推荐阅读
- r - 如何在一个组中应用一个函数,该函数取决于该组中的哪一行保存一个值?
- stripe-payments - 更新 Stripe Price 对象
- html - 如何用导航栏下的侧边栏填充剩余的视口高度?
- javascript - 为什么 css 不适用于锚标签?
- sql-server - 如果使用 docker,SQL Server 中的数据库容量是无限的吗?
- javascript - 无法在“节点”上执行“插入前”:参数 1 在 Javascript 上不是“节点”类型
- javascript - 在 Javascript 中创建一个固定长度的队列数组
- bash - 使用 shell 可执行文件动态更改 Lambda 层的版本号
- javascript - 如何让 Web 服务器在一段时间后或关闭选项卡时删除系统上的文件
- kml - 为什么这个 .kml 文件在零纬度/零经度加载?