【发布时间】:2019-03-12 23:00:03
【问题描述】:
我是 Angular 2+ 的初学者。
我的项目包含一个登录页面,称为主页和关于页面,其中包含与登录页面相关的页眉和页脚。 有一个登录按钮,在登录按钮之后,仪表板的页眉和页脚会发生变化(菜单是隐藏的颜色变化等)。
目前,我在项目中的 app.module.ts 中设置路由如下:
{ path: 'home', component: HomeComponent },
{ path: 'about', component: AboutComponent },
{ path: 'login', component: LoginComponent},
{ path: 'dashboard', component: DashboardComponent},
现在路由工作正常,页眉和页脚加载正常,但是当我导航到 localhost/dashboard 时,登陆页面的页眉和页脚也会出现在那里,这很明显,因为我已经放置了它们在我的 app.component.html 文件中作为
<header-nav></header-nav>
<router-outlet></router-outlet>
<footer></footer>
问题: 我想要实现的是,当在 url 中加载“/dashboard”时,不会加载来自 app.component.html 的页眉和页脚,而是加载来自不同 component.html 文件的自己的页眉和页脚。
方法 1:我可以为每个组件的每个 html 页面添加页眉和页脚,并且只在 app.component.html 中添加 router-outlet,以便在每个页面上加载相应的元素。
我正在寻找一种更好的方法,其中 /dashboard 从不同的 component.html 文件加载,我可以轻松创建登录页面 app.component.html 作为
<header-nav></header-nav>
<router-outlet></router-outlet>
<footer></footer>
dashboard.component.html 为
<dashboard-header></dashboard-header>
<router-outlet></router-outlet>
<footer></footer>
【问题讨论】: