简体   繁体   English

Angular 2:异步显示与解析器相关的子组件

[英]Angular 2 : Displaying resolver dependent child components asynchronously

I have three children components nested in a parent component. 我在父组件中嵌套了三个子组件。 I am trying to have these to display independently, once they resolve their data. 一旦它们解析了数据,我试图使它们独立显示。 So far the complete route is only activated when all the resolves have fetched their data, which results in all children components appearing at once after quite some time (some API calls being long). 到目前为止,只有在所有解析都已获取其数据后才激活完整路由,这导致所有子组件在相当长的一段时间后立即出现(某些API调用很长)。

Here is some simplified code : 这是一些简化的代码:

Routes : 路线:

const appRoutes: Routes = [
  {
    path: '',
    redirectTo: 'home',
    pathMatch: 'full'
  },
  {
    path: 'home',
    component: HomeComponent,
    children : [
      {
        path: 'cpt1',
        component: Component1,
        outlet: 'cpt1',
        resolve: {
          data: Cpt1Resolve
        }
      },
      {
        path: 'cpt2',
        component: Component2,
        outlet: 'cpt2',
        resolve: {
          data: Cpt2Resolve
        }
      },
      {
        path: 'cpt3',
        component: Component3,
        outlet: 'cpt3',
        resolve: {
          data: Cpt3Resolve
        }
      }
    ]
  }
];

App component : 应用程序组件:

@Component({
  selector: 'my-app',
  template: `
      <h1>Angular 2 Asynchronous loading of children components</h1>
      <div>
        <router-outlet></router-outlet>
      </div>`
})
export class AppComponent {}

Home component : 家庭组件:

@Component({
  selector: 'home-cpt',
  template: `
  <h2>You're on home</h2>
  <router-outlet></router-outlet>
  <router-outlet name="cpt1"></router-outlet>
  <router-outlet name="cpt2"></router-outlet>
  <router-outlet name="cpt3"></router-outlet>
  `
})

export class HomeComponent {
   constructor(router: Router) {
    router.navigateByUrl('home/(cpt1:cpt1//cpt2:cpt2//cpt3:cpt3)');
  }
}

And finally a child component and it's resolve : 最后是一个子组件,它可以解决:

// CHILD COMPONENT 1

@Component({
  selector: 'cpt1',
  template: `
  <h3>Cpt1 displayed</h3>
  `
})

export class Component1 implements OnInit {

  constructor(
    private route: ActivatedRoute,
    private router: Router
  ) { }

  ngOnInit(): void {
    this.route.data.forEach((data: any) => {   
      console.log('data cpt 1', data);
    });
  }

}

@Injectable()
export class Cpt1Resolve implements Resolve<any> {
    constructor() {}
        resolve (route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<any> {
            return Observable.create((observer) => {
                  setTimeout(() => { 
                    observer.next('Sample Text 1');
                    observer.complete();
                  }, 2000);
            });
     }
}

Is there a way to force the router to display children components as soon as they receive data ? 有没有一种方法可以迫使路由器在收到子组件后立即显示它们? What am I doing wrong here ? 我在这里做错了什么?

The complete plunker (wait for it) 完整的塞子 (等待它)

Seems that unfortunately displaying a component as soon as the resolver is complete is not possible at the moment and the angular people don't plan on changing it to make it possible. 似乎不幸的是,目前无法在解析器完成后立即显示组件,而且棱角分明的人也不打算更改它以使其成为可能。

https://github.com/angular/angular/issues/14064 https://github.com/angular/angular/issues/14064

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

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