Angular 角度6如何从主体传递选定对象<;对象[]>;使用服务创建组件?

Angular 角度6如何从主体传递选定对象<;对象[]>;使用服务创建组件?,angular,service,rxjs,behaviorsubject,Angular,Service,Rxjs,Behaviorsubject,我有一个使用apiService的类似组件,在这里我使用Behavior Subject来共享API中的数据: settings.component.ts: export class SettingsComponent implements OnInit { data: Observable<Setting[]>; public selectedSetting: Setting; constructor(private apiService: ApiService,

我有一个使用apiService的类似组件,在这里我使用Behavior Subject来共享API中的数据:

settings.component.ts:

export class SettingsComponent implements OnInit {

  data: Observable<Setting[]>;
  public selectedSetting: Setting;

  constructor(private apiService: ApiService,
            private router: Router) { }

  // Load Setting while starting
  ngOnInit() {
    // this.getSettings();
    this.data = this.apiService.fetchData();
  }

  onSelect(setting: Setting): void {
    this.selectedSetting = setting;
  }

  clearSelect(): void {
    this.selectedSetting = null;
  }

  goToSettingDetail(): void {
    this.router.navigate(['settingsDetail']);
  }
}
导出类设置组件实现OnInit{
数据:可观察;
公共选择设置:设置;
建造商(私人apiService:apiService,
专用路由器:路由器{}
//启动时的负载设置
恩戈尼尼特(){
//此参数为.getSettings();
this.data=this.apiService.fetchData();
}
onSelect(设置:设置):无效{
this.selectedSetting=设置;
}
clearSelect():void{
this.selectedSetting=null;
}
goToSettingDetail():void{
this.router.navigate(['settingsdeail']);
}
}
api.service.ts:

export class ApiService {

  private API_URL = 'someUrl';
  public subject$: BehaviorSubject<Setting[]> = new BehaviorSubject<Setting[]>([]);


  constructor(private httpClient: HttpClient) {
  }

  // use async pipe in html template "*ngFor= let s of settings | async"
  // and just inject ApiService in components constructor
  fetchData() {
    const fetch$: Observable <Setting[]> = this.getSettings().pipe(share());
    fetch$.pipe(
        map(allSettings => this.subject$.next(allSettings))
      )
      .subscribe();
    return fetch$;
  }

  /** GET settings from API*/
  getSettings(): Observable<Setting[]> {
    return this.httpClient.get<Setting[]>(this.API_URL + '/settings')
      .pipe(
        catchError(this.handleError('getSettings', []))
      );
  }
}
导出类API服务{
私有API_URL='someUrl';
公共主体$:行为主体=新的行为主体([]);
构造函数(私有httpClient:httpClient){
}
//在html模板“*ngFor=let s of settings | async”中使用异步管道
//只需在组件构造函数中注入ApiService
fetchData(){
const fetch$:Observable=this.getSettings().pipe(share());
获取$.pipe(
映射(allSettings=>this.subject$.next(allSettings))
)
.subscribe();
返回fetch$;
}
/**从API获取设置*/
getSettings():可观察{
返回this.httpClient.get(this.API_URL+'/settings')
.烟斗(
catchError(this.handleError('getSettings',[]))
);
}
}
因此,如果我在html文件中有这样一个部分:

<table>
  <tr>
  <th align="left">Id</th>
  </tr>

  <tr *ngFor="let s of data | async">
    <td>{{s.Id}}</td>
    <td>
      <button (click)="onSelect(s); goToSettingsDetail()">ViewSettings</button>
    </td>
  </tr>
</table>

身份证件
{{s.Id}}
视图设置
仍然使用Behavior Subject获取其他组件中的选定对象的正确方法是什么?如果我使用一个附加服务,它从settingsComponent获取所选对象,它与行为主体不再相关,对吗?所以,若我对其他组件中的选定对象进行更改,并没有人会注意到


你能给我一些提示吗?:)

我会将您选择的设置推送到您的服务中。然后,您可以订阅子组件中的选择

导出类API服务{
private _selectedSetting$:BehaviorSubject=new BehaviorSubject(null);
public selectedSetting$:Observable=此。_selectedSetting$.asObservable();
...
公共设置选定设置(s:设置){
此。\ u选择设置$。下一步;
}
...
}

我会将您选择的设置推送到您的服务中。然后,您可以订阅子组件中的选择

导出类API服务{
private _selectedSetting$:BehaviorSubject=new BehaviorSubject(null);
public selectedSetting$:Observable=此。_selectedSetting$.asObservable();
...
公共设置选定设置(s:设置){
此。\ u选择设置$。下一步;
}
...
}

Ok,因此我调用main.component.ts:
setSelectedSetting(s:Setting){this.apiService.setSelectedSetting(s)}
中的示例,现在在subcomponent.ts:
1中。注入一个服务
2。selectedSetting:Observable=this.apiService.selectedSetting$.subscribe()
是。如果对组件有意义的话,您甚至可以在模板中包含
。`好的,因此我调用main.component.ts:
setSelectedSetting(s:Setting){this.apiService.setSelectedSetting(s)}
作为示例,现在在subcomponent.ts:
1中。注入一个服务
2。selectedSetting:Observable=this.apiService.selectedSetting$.subscribe()
是。如果对组件有意义,您甚至可以在模板中包含
`