【问题标题】:Angular 6 Router Repeating HTMLAngular 6 路由器重复 HTML
【发布时间】:2018-07-06 09:23:58
【问题描述】:

我正在努力实现以下目标:

  1. 我在app.component.html 中有一个mat-toolbar 组件,它显示站点的主顶部导航栏。工具栏下方是<router-outlet></router-outlet>
  2. 当用户导航到localhost:4200/ 的根目录时,我希望顶部导航栏可见,并且与导航栏中的链接关联的所有组件都呈现在顶部导航栏下。
  3. 现在的问题是,当用户导航到根目录时,顶部导航栏会重复两次。单击链接,重复的导航栏被删除,并在导航栏下呈现正确的组件(这是我想要的,没有重复)。

这里是app.module.ts

// initialization and module imports
const RootRoutes: Routes = [
 {path: "", redirectTo: "root", pathMatch: "full"},
 {path: "root", component: AppComponent, },
 //{path: "home", component: HomeComponent },
 {path: "fiction", component: FictionDisplayComponent },
 {path: "nonfiction", component: NonFictionDisplayComponent},
 {path: 'poetry', component: PoetryDisplayComponent},
 {path: 'journal', component: JournalDisplayComponent},
 {path: 'letters', component: PersonalLetterDisplayComponent},
 {path: 'jokes', component: JokesDisplayComponent}
];
// NgModule declaration

app.component.html 的设置如下:

<div class="pure-g rr-main-wrapper">
    <div class="pure-u-5-5 home-top-navbar-wrapper">
        <rr-home-top-navbar></rr-home-top-navbar>
    </div>

</div> 
<router-outlet></router-outlet>

home-top-navbar.component.html 的设置方式如下:

<mat-toolbar color="primary" class="home-top-nav">
  <mat-toolbar-row>
    <img src="./../../assets/imgs/logo2.png" width="200" height="50">
    <mat-nav-list class="home-top-nav">
      <mat-list-item *ngFor="let route of navbarRoutes">
        <a [routerLink]="[route.url]" class="navbar-link">{{ route.name }}</a>
      </mat-list-item>
    </mat-nav-list>
  </mat-toolbar-row>

现在,如果您注意到有注释掉的路径 {path: 'home', component: HomeComponent},我尝试单独加载工具栏,并且在 app.component.html 中只有 &lt;router-outlet&gt;&lt;/router-outlet&gt;。但是,问题是当我单击链接时,页面导航到更正的组件,
顶部导航栏不再可见(这是预期的,因为其他组件不会重复导航栏代码)。

当用户导航到根目录时,有什么方法可以在不重复工具栏两次的情况下实现这一点?这可以在不复制/粘贴顶级组件的每个页面上的导航栏的情况下完成吗?

【问题讨论】:

  • 从@Benyamin 在他关于不正确路线的回答中提到的内容继续,你有AppRoutingModule吗?
  • @mittenspair 下面的 bunyamicoskuner 提供的解决方案实际上解决了这个问题。谢谢你的回复。
  • 不用担心。太棒了,您的问题这么快就解决了!
  • 重复的stackoverflow.com/questions/39277700/angular-2-duplicate-headerstackoverflow.com/questions/40198685/… 这个重复的标题问题是重复标题的重复。你得到我的副本吗? ;)
  • @mittenspair 我之前确实看过那篇文章。仍然想用一个单独的问题来澄清。

标签: angular angular-router


【解决方案1】:

AppComponent 不应成为您的Routes 的一部分。这是因为,AppComponent 是您的应用程序的入口点。在index.html 中,您有类似&lt;rr-app&gt;&lt;/rr-app&gt; 的内容。如果存在导航到AppComponent 的路由(路由root),Angular 将在router-content 内渲染AppComponent,最终生成两个navbar,仅此而已。

据我了解,您不需要路由root

将您的路由配置更改为以下。

/* 
 * Removed AppComponent and redirected "/" to "home"
 */
const RootRoutes: Routes = [
 {path: "", redirectTo: "home", pathMatch: "full"},
 {path: "home", component: HomeComponent },
 {path: "fiction", component: FictionDisplayComponent },
 {path: "nonfiction", component: NonFictionDisplayComponent},
 {path: 'poetry', component: PoetryDisplayComponent},
 {path: 'journal', component: JournalDisplayComponent},
 {path: 'letters', component: PersonalLetterDisplayComponent},
 {path: 'jokes', component: JokesDisplayComponent}
];

【讨论】:

  • 不错的收获!路由通常不是在单独的常量路由文件中吗?觉得有些不同。
  • @BunyaminCoskuner 按预期工作。感谢您的快速回复。
猜你喜欢
  • 2018-11-19
  • 2018-12-27
  • 1970-01-01
  • 2022-09-24
  • 2019-01-05
  • 2018-12-15
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
相关资源
最近更新 更多