简体   繁体   English

观察者订阅第一次没有加载,第一次加载到组件时没有触发

[英]Observer subscribe doesn't load first time, not firing on first load to Component

I have a fetch-person component which loads some data in perList: Observable<Person[]> .我有一个 fetch-person 组件,它在perList: Observable<Person[]>中加载一些数据。 Then at first time the data doesn't load to the this.personList (from login page to fetch-person component RouterModule.forRoot([{ path: '', redirectTo: '/fetch-person', pathMatch: 'full'}) However, After I click on other page and click on header <a class="nav-link text-dark" [routerLink]='["/fetch-person"]'>Fetch Person</a> , it would return the data to the this.personList . I came up with my code (code is in Typescript):然后第一次数据不会加载到this.personList (从登录页面到 fetch-person 组件RouterModule.forRoot([{ path: '', redirectTo: '/fetch-person', pathMatch: 'full'})但是,在我点击其他页面并点击 header <a class="nav-link text-dark" [routerLink]='["/fetch-person"]'>Fetch Person</a>后,它会返回this.personList的数据。我想出了我的代码(代码在 Typescript 中):

How can I get the data from the first load?如何从第一次加载中获取数据?

fetch-person.component.ts

@Component({
  selector: 'app-fetch-person',
  templateUrl: './fetch-person.component.html',
  styleUrls: ['./fetch-person.component.css']
})

@Injectable()
export class FetchPersonComponent implements OnInit {
    [x: string]: any;

  @ViewChild(MatPaginator, { static: false }) paginator: MatPaginator;

  SearchpersonForm: FormGroup;
  loading$: Observable<Boolean>;
  error$: Observable<Error>
  public perList: Observable<Person[]>;
  personList: Person[];
  pagedList: Person[] = [];
  breakpoint: number = 3; 
  length: number = 0;
  pageSize: number = 25;  //displaying three cards each row
  pageSizeOptions: number[] = [5, 10, 25, 100];

  //, private decimalPipe: DecimalPipe
  constructor(private _fb: FormBuilder, private store: Store<AppState>) {
    this.SearchpersonForm = this._fb.group({
      name: ['', [Validators.required]]
    })
  }
  
 ngOnInit() {
      this.store.dispatch(FetchPerson());
     this.perList = this.store.pipe(select(getPersons));
     this.perList.subscribe(result => { this.personList = result; } );<-- **there is no data for the first load from redirectTo: '/fetch-person', however, there are some data after second load from [routerLink]='["/fetch-person"]'**
      this.pagedList = this.personList;
     this.length = this.personList.length;
 }

The issue with your code is that the subscribe is an observable and runs asynchronously.您的代码的问题是订阅是可观察的并且异步运行。 So the code below the subscribe executes prior to the the population of the variable.因此,订阅下方的代码在填充变量之前执行。

I would also subscribe before dispatching, just for clarity.为了清楚起见,我也会在发送前订阅。

 ngOnInit() {
      this.store.pipe(select(getPersons))
                .subscribe(result => { 
                                      this.personList = result;
                                      this.pagedList = this.personList;
                                      this.length = this.personList.length; 
                                   });

      this.store.dispatch(FetchPerson());
  
 }

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

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