【问题标题】:ERROR TypeError: Cannot read property 'filter' of undefined in ionic 4错误类型错误:无法读取离子 4 中未定义的属性“过滤器”
【发布时间】:2020-02-08 05:05:49
【问题描述】:

这是错误信息: 错误类型错误:无法读取未定义的属性“过滤器”

HTML

<ion-searchbar (ionChange)=search($event) color='medium'></ion-searchbar>

ts

    allMessages: message[];
    messages : message[];


  search(event) {
  const text = event.target.value;
  if (text && text.trim() !== '') {
    this.room.messages = this.allMessages.filter(
      message => message.content.toLowerCase().includes(text.toLowerCase()));
  } else {
    // Blank text, clear the search, show all products
    this.room.messages = this.allMessages;
  }
}

【问题讨论】:

    标签: angular ionic4


    【解决方案1】:

    您的数组this.allMessages 必须在过滤之前填充。它似乎没有任何价值。您已定义变量但尚未声明它。你必须像

    一样声明它
    allMessages: message[] = [];
    

    【讨论】:

    • 您是从中删除值还是在其他地方重新声明?
    【解决方案2】:

    首先你必须正确初始化你的变量,然后检查当模板初始化已经完成时事件没有触发,之前有一个问题。

    如果一切正常,快速修复可能会执行以下操作:

    this.room.messages = this.allMessages && this.allMessages.filter(...someCode) || [] 
    

    您也可以使用 isNullOrUndefined() 方法或三元条件检查第一条语句

    【讨论】:

      猜你喜欢
      • 2019-08-26
      • 2021-12-31
      • 1970-01-01
      • 2019-03-13
      • 1970-01-01
      • 2020-05-28
      • 1970-01-01
      • 2018-01-22
      相关资源
      最近更新 更多