我编写了一个名为“客户”的模块,它有几个组件,如登录、主页和注册。现在我创建了一个共享模块,它也有 2 个组件,例如页眉和页脚。由于页眉和页脚将由客户模块中的所有组件共享,因此我将它们放在共享模块中。共享模块被导入到客户模块。
现在有一个路由器链接指向客户模块内的组件。那些路由器链接不会被解释为 href。但是,如果我将这些页眉和页脚组件直接放在客户模块中,那么这些路由器链接就会被解释。
我在下面包含了代码片段。
共享模块文件
import { NgModule } from '@angular/core';
import { HeaderComponent } from './header/header.component';
import { FooterComponent } from './footer/footer.component';
@NgModule({
imports: [ ],
declarations: [ HeaderComponent, FooterComponent ],
exports: [ HeaderComponent, FooterComponent ]
})
export class SharedModule { }
客户模块文件
import { NgModule } from '@angular/core';
import { SharedModule } from './shared/shared.module';
import { CustomerRoutingModule } from './customer-routing.module';
import { CustomerComponent } from './customer.component';
import { CustomerHomeComponent } from './home/home.component';
import { CustomerLoginComponent } from './login/login.component';
import { CustomerRegisterComponent } from './register/register.component';
@NgModule({
imports: [ SharedModule, CustomerRoutingModule ],
declarations: [ CustomerComponent, CustomerHomeComponent, CustomerLoginComponent, CustomerRegisterComponent ]
})
export class CustomerModule { }
头组件html
<div class="header-wrapper">
<nav class="navbar navbar-light bg-faded">
<button class="navbar-toggler hidden-lg-up" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation"></button>
<div class="collapse navbar-toggleable-md" id="navbarResponsive">
<a class="navbar-brand header-logo" routerLink="./">Your space your time</a>
<ul class="nav navbar-nav header-menu float-lg-right">
<li class="nav-item header-menu-item">
<a class="nav-link header-menu-link" href="#">About</a>
</li>
<li class="nav-item header-menu-item">
<a class="nav-link header-menu-link" href="#">Services</a>
</li>
<li class="nav-item header-menu-item">
<a class="nav-link header-menu-link" routerLink="./signin" routerLinkActive="active">Login <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item header-menu-item">
<a class="nav-link header-menu-link" routerLink="./signup" routerLinkActive="active">Register</a>
</li>
</ul>
</div>
</nav>
</div>
客户路由模块
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { CustomerComponent } from './customer.component';
import { CustomerHomeComponent } from './home/home.component';
import { CustomerLoginComponent } from './login/login.component';
import { CustomerRegisterComponent } from './register/register.component';
const customerRoutes: Routes = [
{ path: '', redirectTo: 'customer', pathMatch: 'full' },
{ path: 'customer', component: CustomerComponent,
children: [
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', component: CustomerHomeComponent },
{ path: 'signin', component: CustomerLoginComponent },
{ path: 'signup', component: CustomerRegisterComponent }
]
}
];
@NgModule({
imports: [
RouterModule.forChild(customerRoutes)
],
exports: [
RouterModule
]
})
export class CustomerRoutingModule { }