我想知道如何才能尽可能地在跨Angular的多个组件之间进行搜索。所有将要定位的组件都经过硬编码。
我已经尝试在Google和其他各个位置四处寻找,唯一的结果是针对数据库的搜索,对于我的应用程序而言并非如此。
您可以在其中提供带有BehaviorSubject的服务。它将作为一个全局状态,并且注入服务的每个组件都将操纵可观察的或观察更改。
@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([...])
}
}