如何使用Angular 2 Router导航到页面的特定部分?

问题描述:

我正在尝试导航到Angular 2组件中的特定元素.从我阅读的文档中可以看出,片段与NavigationExtras对象一起使用.当我要在同一页面中导航时,我不确定如何执行此操作.

I'm trying to navigate to a particular element in my Angular 2 component. From the documentation I've read, it says to use fragment with the NavigationExtras object. I'm not sure how to do this when I want to navigate within the same page.

问题的另一部分是我要导航到的页面部分通过* ngFor加载,并使用我要导航的id创建div元素.

Another part of the problem is the part of the page I want to navigate to is loaded through trough a *ngFor and creating div elements with the id I want to navigate to.

在将其加载到DOM中之前,我可能试图导航至该页面的该部分.我在它周围设置了一个超时,以查看是否有帮助,但是到目前为止还没有成功.

I may be trying to navigate to that part of the page before it's loaded into the DOM. I wrapped a timeout around it to see if that would help, but so far not successful.

我在属性页上,并尝试导航到页面内的另一部分.您可以在HTML中看到我在需要导航到的div上设置id的位置.

I'm on the properties page, and trying to navigate to another part within the page. You can see in the HTML where I'm setting the id on the div where I need to navigate to.

    if (sessionStorage['destinationProperty'] !== undefined) {
        let navigationExtras: NavigationExtras = {
        fragment: sessionStorage.getItem('destinationProperty')
      }

      window.setTimeout(() => {
        this._router.navigate(['/properties'], navigationExtras);
      }, 1000);
    }

    <div class="row" *ngFor="let p of properties">
      <div class="col-md-12">
          <div class="row">
              <div class="col-md-3"></div>
              <div class="col-md-6" id="{{p.id}}">
                <table>
                    <tr>
                        <td rowspan="3"><img [src]="getImgPath(p)" /></td>
                        <td class="title" (click)="destination(p)">{{p.name}}</td>
                    </tr>
                    <tr>
                        <td class="summary">{{p.summary}}</td>
                    </tr>
                    <tr>
                        <td><span class="mapLink" (click)="mapview(p)">view on map <i class="fa fa-map-o" aria-hidden="true"></i></span></td>
                    </tr>
                </table>
              </div>
              <div class="col-md-3"></div>
          </div>
      </div>
    </div>

  • 在Angular2应用中,您可以为某些组件设置路由器.

    • In Angular2 app you can have a router for some components.

      每个组件都可以拥有自己的其他组件路由器,依此类推.

      Each component can have its own router for other components and so on.

      因此,实际上您的主路由器需要一个子路由器.
      这是Angular2的示例文档处理儿童路由器:
      主路由器

      So you actually need a child router for your primary router.
      Here it's an example from Angular2 docs of how they handle children routers :
      Master router

      import { ModuleWithProviders }  from '@angular/core';
      import { Routes, RouterModule } from '@angular/router';
      
      export const routes: Routes = [
        { path: '', redirectTo: 'contact', pathMatch: 'full'},
        { path: 'crisis', loadChildren: 'app/crisis/crisis.module#CrisisModule' },
        { path: 'heroes', loadChildren: 'app/hero/hero.module#HeroModule' }
      ];
      
      export const routing: ModuleWithProviders = RouterModule.forRoot(routes);
      

      子路由器

          import { ModuleWithProviders } from '@angular/core';
          import { Routes,
                   RouterModule } from '@angular/router';
      
          import { HeroComponent }       from './hero.component';
          import { HeroListComponent }   from './hero-list.component';
          import { HeroDetailComponent } from './hero-detail.component';
      
          const routes: Routes = [
            { path: '',
              component: HeroComponent,
              children: [
                { path: '',    component: HeroListComponent },
                { path: ':id', component: HeroDetailComponent }
              ]
            }
          ];
      
      export const routing: ModuleWithProviders = RouterModule.forChild(routes);
      

      链接到完整示例:
      路由器的Angular2延迟加载
      Angular2完整工作的监听器,用于通过路由器延迟加载

      Links to full example :
      Angular2 lazy loading with the router
      Angular2 full working plunker for lazy loading with the router