简体   繁体   English

如何使用角度路由器导航到各个部分?

[英]How to navigate into sections with angular router?

How can I navigate to a specific section of an HTML on Angular 2?如何在 Angular 2 上导航到 HTML 的特定部分? I want to navigate as follows:我想导航如下:

<a href="#first">First section</a></li>
<a href="#second">Second section</a></li>
<a href="#third">Third section</a></li>

<section id="first">
  Content...
</section <section id="second">
Content...
</section>
<section id="third">
  Content...
</section>

but with Angular router.但使用 Angular 路由器。

You need to add this in your router你需要在你的路由器中添加这个

const routerOptions: ExtraOptions = {
    scrollPositionRestoration: 'enabled',
    anchorScrolling: 'enabled',
    scrollOffset: [0, 64],
  };




@NgModule({
    imports: [CommonModule, RouterModule.forRoot(routes, routerOptions)], //ExtraOptions
    ...
  })

And Your anchor tag should be like this你的锚标签应该是这样的

<a 
  [routerLink]="['/yourPageLink', params]" fragment="first}">First section</a>

For Doc Click here文档请点击这里

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

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