html - Angular 6链接到其他组件中的部分?

标签 html angular angular6

我有一个带有此代码的导航栏组件:

 <ul class="navbar-nav mx-auto">
              <li class="nav-item active">
                <a class="nav-link" href="#" >Inicio <span class="sr-only"></span></a>
              </li>
              <li class="nav-item active">
                <a class="nav-link" href="#">Here Link Home Section 2 <span class="sr-only"></span></a>
              </li>


</ul>

还有另一个组件 HomeComponent 有 3 个部分:

<section id="home_one">
    stuff 
    section
    one
</section>

<section id="home_two">
   stuff 
   section
   two
</section>

<section id="home_three">
    stuff 
    section
    three
</section>

如何从导航栏链接到另一个组件 (homeComponent) 中的特定部分(第 2 部分)?

最佳答案

Angular 6.1 有一个叫做 anchorScrolling 的新功能,启用这个选项并使用 Router fragment

app.module.ts

@NgModule({
  imports: [BrowserModule, FormsModule, RouterModule.forRoot(routes, {
    anchorScrolling: 'enabled',
  })],
  declarations: [AppComponent, HelloComponent, ComponentbComponent],
  bootstrap: [AppComponent]
})

app.component.html

<a [routerLink]="['item']" [fragment]="'section2'">
    section2
</a>
<router-outlet></router-outlet>

示例:https://stackblitz.com/edit/angular-zgmsuw

关于html - Angular 6链接到其他组件中的部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53558945/

相关文章:

javascript - 单选按钮拦截 Angular/Javascript 中未经检查的事件

angular - 比较两个大对象的 Angular 会导致渲染速度变慢

angular - 如何在 Angular 6 中构建具有多个组件的表单?

SignalR SSE 握手失败

html - 自定义字体,eot,不工作

javascript - 根据宽度切换点击和悬停事件

php - 如何使用 Jquery 或 PHP 打印关联的 div 内容和 css

angular - 如何在 Angular 组件 HTML DOM 中注入(inject) SVG 图标 Sprite ?

angular - Angular 6 中 ngIf 的多个条件

javascript - 单击浏览器 html 页面中的链接时显示 VB6 表单