![](/img/trans.png)
[英]Angular2/4: How to share property values across multiple components?
[英]How to search through static data across multiple Angular components?
我想知道,如果可能的話,我如何設置一個可以跨 Angular 中的多個組件工作的搜索。 所有將成為目標的組件都是硬編碼的。
我已經嘗試在 google 和其他各種位置上環顧四周,唯一的結果是使用數據庫進行搜索,而我的應用程序並非如此。
您可以在其中使用 BehaviorSubject 進行服務。 它將充當全局 state 並且注入服務的每個組件都將操縱可觀察或觀察變化。
@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([...])
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.