1. 准备两个父路由组件
2. 在两个 父路由组件下创建两个子路由组件
3. 注册四个组件
4.在app-routing.module.ts中注册写入
const routes: Routes = [
{
path:'parent',
component:ParentComponent,
// redirectTo:'/welcome',
children:[
{
path:'welcome',
component:WelcomeComponent
},
{
path:'setting',
component:SettingComponent
},
{
path:'**',
redirectTo:'welcome'
}
]
},
{
path:'child',
component:ChilldComponent,
children:[
{
path:'plist',
component:ProductlistComponent
},
{
path:'pcate',
component:ProductcateComponent
},
{
path:'**',
redirectTo:'plist'
}
]
},
{
path:'**',
redirectTo:'parent'
}
];
5. 在app.component.html中设置两个父路由的切换,并设置路由的位置
<a [routerLink]="[ '/parent' ]">首页</a>
<a [routerLink]="[ '/child' ]">商品</a>
<router-outlet></router-outlet>
6.在首页组件中
<div class="content">
<div class="left">
<!-- <a href="#">欢迎首页</a> -->
<a [routerLink]="[ '/parent/welcome' ]">欢迎首页</a>
<!-- <a href="#">系统设置</a> -->
<a [routerLink]="[ '/parent/setting' ]">系统设置</a>
</div>
<div class="right">
<router-outlet></router-outlet>
</div>
</div>
7.在商品路由中
<div class="content">
<div class="left">
<!-- <a href="#">商品分类</a> -->
<a [routerLink]="[ '/child/pcate', ]">商品分类</a>
<!-- <a href="#">商品列表</a> -->
<a [routerLink]="[ '/child/plist' ]">商品列表</a>
</div>
<div class="right">
<router-outlet></router-outlet>
</div>
</div>
8.在公共样式中
.content{
width: 100%;
height: 500px;
padding: 10px;
display: flex;
.left{
width: 200px;
height: 500px;
border-right: 1px solid #eee;
}
.right{
flex: 1px;
}
a{
display: block;
padding: 10px 4px;
}
}