从字面上看,Angular13 非常新。我们的应用程序在启动时需要一些配置数据,因此在 AppComponent 上调用共享服务,其数据将围绕应用程序页眉、菜单和页脚共享,以显示/隐藏一些 div。下面是 SharedService 的示例代码。 共享服务.ts
import { HttpClient } from '@angular/common/http';
import { BehaviorSubject, Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class SharedService {
private data: BehaviorSubject<any> = new BehaviorSubject<any>(null);
data$: Observable<any> = this.data.asObservable();
private companyLogo: string = '';
private languages: string = 'en-us';
constructor(private http: HttpClient) {
this.http.get('/shared').subscribe(data => {
//let us say data will be like
data = {
"logourl" : '/img/mg1.jpg',
"showDashboard": true,
"language": 'en',
"username": 'user-1', //so on and it has 20+ values
}
//How to structure this data and called throughout application
})
}
}```
I have no idea that how to use return data whether to get it through observables/getter/setter. Guide me in implementing this.