{
// 0代表正常,其他则异常
status: 0,
// 商品列表
message: [
{
id: 1,
name: '奔驰',
ctime: '2017-08-26T08:37:18.000Z'
},
{
id: 2,
name: '宝马',
ctime: '2017-08-25T06:32:14.000Z'
},
...
]
}
{
// 0代表正常,其他则异常
status: 0,
// 删除结果
message: "删除品牌数据ok"
}
{
// 0代表正常,其他则异常
status: 0,
// 添加结果
message: "添加成功"
}
ng g component home
ng g component news
ng g component newscontent
import { NgModule } from '@angular/core';import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './home/home.component';import { NewsComponent } from './news/news.component';
import { NewscontentComponent } from './newscontent/newscontent.component';
const routes: Routes = [ //路由组件是一个数组,每一个配置项为一个对象,分别有path(路径),compontent(组件)等属性 {path: 'home', component: HomeComponent},
{path: 'news', component: NewsComponent},
{path: 'newscontent/:id', component: NewscontentComponent},
{ path: '', redirectTo: '/home', pathMatch: 'full' } //重定向,如果用户打开路径为''的时候自动重定向到home组件下;
];
@NgModule({ imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
import { AppRoutingModule } from './app-routing.module';
imports: [ BrowserModule,
AppRoutingModule
]
<h1> <a routerLink="/home">首页</a>
<a routerLink="/news">新闻</a>
</h1>
<router-outlet></router-outlet>
//匹配不到路由的时候加载的组件 或者跳转的路由
{ path: '**', /*任意的路由*/
redirectTo:'home'
}
<h1>
<a routerLink="/home" routerLinkActive="active">首页</a>
<a routerLink="/news" routerLinkActive="active">新闻</a>
</h1>
.active{
color:red;
}
const routes: Routes = [ {path: 'home', component: HomeComponent},
{path: 'news', component: NewsComponent},
{path: 'newscontent/:id', component: NewscontentComponent}, // id是不固定的,所以当一个参数传过去
{ path: '', redirectTo: '/home', pathMatch: 'full' }
];
import { Router, ActivatedRoute, Params } from '@angular/router'; //引入路由模块constructor( private route: ActivatedRoute) {}
ngOnInit() {
console.log(this.route.params);
this.route.params.subscribe(data=>this.id=data.id); //通过subscribe方法订阅路由参数
}
import { Router } from '@angular/router';
export class HomeComponent implements OnInit { constructor(private router: Router) { }
ngOnInit() { } goNews(){
this.router.navigate(['/news']); }//页面初始化的时候跳转到/news路由对应的页面下
}
this.router.navigate(['/news', hero.id]);
//父子路由配置如下{ path: 'news', component:NewsComponent,
children: [
{ path:'newslist', component:NewslistComponent },
{ path:'newsadd', component:NewsaddComponent }
]
}
> 父子路由要在父路由组件中定义`router-outlet`
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |