简体   繁体   English

如何正确使用异步管道来更新 angular 7 中的模板

[英]How can I use async pipe correctly to update template in angular 7

I have a service method which gets dummy data and returns an observable.我有一个服务方法,它获取虚拟数据并返回一个可观察的。

private dummySubject = new BehaviorSubject<Dummy>(null);
dummy$ = this.dummySubject.asObservable();

loadDummyData(id: string): Observable<Dummy> {
  const BASE_URL = 'someurl/' + id; 
  return this.http.get<Dummy>(BASE_URL).pipe( 
    tap((dummyData: Dummy ) => {
      console.log('In Service ', dummyData); //LOGS DATA HERE
      this.dummySubject.next(dummyData);
    })
  );
}

In another service, I use the switchMap operator to map a user id into my loadDummyData method.在另一个服务中,我使用 switchMap 运算符将用户 ID 映射到我的loadDummyData方法中。

anotherService另一个服务

loadDummyData(){
  this.dummyService.user$.pipe(
    switchMap((user: User) => {
      return this.dummyService.loadDummyData(user.id);
    })
  ).subscribe();
}

Then in my getData component然后在我的getData component

localData: Dummy;
dummy$: Observable<Dummy> = this.dummyService.dummy$;

ngOnInit() {
  1) this.anotherService.loadDummyData(); //Causes data to be loogged in service
  2) this.dummyService.loadDummyData('12345').subscribe( //causes data logged in service
    (dummy: Dummy) => {
      this.localProfile = dummy;
      console.log(this.localProfile, 'data in component'); //local data log
    }, 
    err => {
      console.error(err);
    }
  );
  console.log(this.localProfile, 'In ngOnint'); //undfiened 
}

In the getData component , I make 2 calls to the dummyService method,getData component ,我对 dummyService 方法进行了 2 次调用,

  1. call anotherService => loadDummyData() (which uses switch map to load data)调用 anotherService => loadDummyData() (它使用 switch map 加载数据)
  2. Call dummyService => loadDummyData() directly passing an Id and subscribing.调用 dummyService => loadDummyData() 直接传递一个 Id 并订阅。

both times the data is logged in the console and seems to be coming back.两次数据都记录在控制台中,似乎又回来了。 However, in my template, the async |但是,在我的模板中, async | is not displaying the data.不显示数据。 The loadProfile variable is also getting undefined in the second call to get the data. loadProfile变量在第二次获取数据的调用中也未定义。

Template模板

<div *ngIf="dummy$ | async as dummyData">
  <span class="pull-right" *ngIf="dummyData.canViewAdmin">
    <a (click)="openChangeUserModal()">Change user</a>
  </span>
  <p>{{dummyData.name}}</p>
</div>

What am I missing here?我在这里缺少什么?

You are not assigning dummyData anywhere!您没有在任何地方分配 dummyData!

Change to this:改成这样:

loadDummyData(): Observable<Dummy> {
  return this.dummyService.user$.pipe(
    switchMap((user: User) => this.dummyService.loadDummyData(user.id)),
  );
}

and

dummy$: Observable<Dummy> = this.dummyService.dummy$;

ngOnInit() {
  this.dummy$ = this.anotherService.loadDummyData();

Since you haven't put up your complete code, the issue that's evident over here is that dummy$ is not assigned the expected value in dummyService.由于您还没有提供完整的代码,这里很明显的问题是 dummy$ 未在 dummyService 中分配预期值。 You'll have to assign value in dummy$ like this.dummy$ = this.dummySubject.asObservable();您必须像this.dummy$ = this.dummySubject.asObservable();一样在 dummy$ 中this.dummy$ = this.dummySubject.asObservable(); then only you can use its value in component.那么只有你可以在组件中使用它的值。

And where is load loadUserProfile method.加载 loadUserProfile 方法在哪里。

In your case if you remove async pipe and bind your html with localProfile, then too it would work.在您的情况下,如果您删除异步管道并将您的 html 与 localProfile 绑定,那么它也可以工作。

Change your html改变你的html

 <div *ngIf="localProfile">
  <span class="pull-right" *ngIf="localProfile.canViewAdmin">
    <a (click)="openChangeUserModal()">Change user</a>
  </span>
  <p>{{localProfile.name}}</p>
</div>

Outside the subscribe callback, localProfile will be undefined because its async call.在订阅回调之外,localProfile 将是未定义的,因为它的异步调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM