【问题标题】:How to search through static data across multiple Angular components?如何跨多个 Angular 组件搜索静态数据?
【发布时间】:2020-05-11 16:36:11
【问题描述】:

我想知道,如果可能的话,我如何设置一个可以跨 Angular 中的多个组件工作的搜索。所有将成为目标的组件都是硬编码的。

我已经尝试在 google 和其他各种位置四处查看,唯一的结果是使用数据库进行搜索,而我的应用程序并非如此。

【问题讨论】:

    标签: angular search static angular9 static-data


    【解决方案1】:

    您可以使用 BehaviorSubject 来创建服务。它将充当全局状态,并且每个注入服务的组件都将操纵 observable 或观察变化。

    @Injectable({providedIn: 'root'})
    public class Service {
    items BehaviorSubject<any[]>
    
      constructor() {
        this.items = new BehaviorSubject([...]);
      }
    }
    
    public class Component1 {
      constructor(private _service: Service) {
        this._service.items.subscribe(pr => {})
      }
    }
    
    public class Component2 {
      constructor(private _service: Service) {}
    
      search() {
        this._service.items.next([...])
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多