You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular集成Bootstrap Typeahead服务报错:找不到searchList

解决Angular中Bootstrap Typeahead的TS2304错误

你遇到的error TS2304: Cannot find name 'searchList'问题很明确——在你的search方法里,你直接引用了searchList,但它是组件的类成员变量,必须通过this来访问才行。

修复后的组件代码

export class NgbdTypeaheadBasic {
  public model: any;
  searchList: Search[];

  constructor(private searchservice: SearchService) { }

  ngOnInit() {
    this.searchList = this.searchservice.getSearch();
  }

  search = (text$: Observable<string>) =>
    text$.pipe(
      debounceTime(200),
      distinctUntilChanged(),
      map(term => term.length < 2 ? [] : 
        // 这里要加上this.来访问组件的searchList
        this.searchList.filter(v => v.searchitem.toLowerCase().indexOf(term.toLowerCase()) > -1).slice(0, 10))
    );
}

原因解释

在TypeScript的类方法中,访问类的实例属性必须使用this关键字。你之前的代码里直接写searchList,TypeScript会把它当作一个局部变量或者全局变量来查找,自然找不到对应的定义,所以抛出了找不到名称的错误。

另外,也请确认你已经将Search类正确导入到组件文件中,避免出现其他类型找不到的衍生问题。


内容的提问来源于stack exchange,提问作者Akshay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:54:26