angular 路由 Router
一、Routers(路由配置),接收一个路由配置数组。 属性
实例: const AppRoutes: Routes = [ {path: '',redirectTo: '/app/dashboard',pathMatch: 'full'},//页面启动首页显示 {path: 'app',component: LayoutComponent},{path: 'demoHouseList',component: HouseListComponent},{path: 'houseMarket',loadChildren: './house-market/houseMarket.module#HouseMarketModule'}//houseMarket后面还要子菜单,在houseMarket-routing.module配置中 houseMarket-routing.module.ts配置: const Routing: Routes = [ { path: '',component: HouseMarketComponent,children: [ {path: 'list',component: ListComponent},{path: 'search',component: SearchComponent} ] } ]; 页面访问: 二、指令 RouterOutlet--相当于一个占位符,在Angular中根据路由状态动态插入视图。 如何使用 <router-outlet></router-outlet> <router-outlet name='left'></router-outlet> <router-outlet name='right'></router-outlet> 一个路由插座可以在任何时候组件实例化时发出一个activate消息,并且在组件销毁时发出一个deactivate消息。 <router-outlet (activate)="onActivate($event)" (deactivate)="onDeactivate($event)"></route-outlet> RouterLink指令能够链接到应用中指定区域。如果使用静态链接,可以像下面这样直接使用: <a routerLink='/user'>link to user component</a> 如果是动态生成的链接,可以传递一个数组带有路径片段,以及后续每一个参数的片段。 多个静态片段可以合并到一个(示例:['/team/11/user'],username,{details: true}) 第一个片段名可以使用/,./,或者 ../ 开关:
使用以下方式设置查询参数和片段 <a [routerLink]="['/user/bob']" [queryParams]="{debug: true}" fragment="education">link to user component</a> 这会生成链接 /user/bob#education?debug=true 三、接口 ActivatedRoute(当前激活路由对象,主要用于保存路由,获取路由传递的参数。) 如何使用 @Component({templateUrl:'./my-component.html'}) class MyComponent { constructor(**route: ActivatedRoute**) { const id: Observable<string> = route.params.map(p => p.id); const url: Observable<string> = route.url.map(s => s.join('')); const user = route.data.map(d => d.user); //includes `data` and `resolve` } } 类定义 interface ActivatedRoute { snapshot : ActivatedRouteSnapshot url : Observable<UrlSegment[]> params : Observable<Params> queryParams : Observable<Params> fragment : Observable<string> data : Observable<Data> outlet : string component : Type<any>|string routeConfig : Route root : ActivatedRoute parent : ActivatedRoute firstChild : ActivatedRoute children : ActivatedRoute[] pathFromRoot : ActivatedRoute[] toString() : string } 属性
传递参数方式,以及ActivatedRoute获取他们的方式 1、在查询参数中传递数据: /product?id=1&name=2 //传参 2、在路由路径中传递数据: {path:'/product/:id'} //路由配置 3、在路由配置中传递数据 {path:'/product',component:ProductComponent,data:[{isProd:true}]} //路由配置 接收参数: import {ActivatedRoute,Router} from '@angular/router'; export class HousedetailComponent implements OnInit { houseMarketId: any; constructor(private route: ActivatedRoute){} ngOnInit() { this.houseMarketId = this.route.snapshot.params('id');或 this.houseMarketId = this.route.snapshot.paramMap.get('id'); } } 现已list列表页点击跳转到详情页来实例ActivatedRoute用法: 在路由路径中传递数据:{path: 'detail/:id',component: detailComponent} 一、list.component.ts和detail.component.ts文件都引入 import {ActivatedRoute,Router} from '@angular/router'; constructor(private route: ActivatedRoute,private router: Router){ } 二、list.component.ts中的点击事件: goDetail(houseMarketId) { const url = '/detail/' + houseMarketId; let queryParams; queryParams = {source: 'list'}; this.router.navigate([url],{relativeTo: this.route,queryParams: queryParams}); } 访问路径:http://localhost:400/detail/e028606e317c11e896ef7cd30adbbf4c?source=list 三、detail.component.ts在初始化时,取的url路由带过来的id值,来取该id对应信息,即:this.route.snapshot.paramMap.get('id') ngOnInit() { this.houseMarketId = this.route.snapshot.paramMap.get('id'); this.housedetailService.getById(this.houseMarketId,this.userId ? this.userId : '') .then((data) => { if (data.code === 200) { this.houseMarket = data.data; } }); } 参考:https://www.kancloud.cn/wujie... (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |