angular中的动态路由

1.配置动态路由

 

const routes: Routes = [
  {path: 'home', component: HomeComponent},
  {path: 'news', component: NewsComponent},
  {path: 'newscontent/:id', component: NewscontentComponent},
  {
    path: '',
    redirectTo: '/home',
    pathMatch: 'full'
} ];

 

2.跳转传值

<a [routerLink]="[ '/newscontent/',aid]">跳转到详情</a> 
<a routerLink="/newscontent/{{aid}}">跳转到详情</a>

3.获取动态路由的值

import { ActivatedRoute} from '@angular/router';
   constructor( private route: ActivatedRoute) { }
ngOnInit() {
  console.log(this.route.params);
  this.route.params.subscribe(data=>this.id=data.id);
}

 


动态路由的 js 跳转

1. 引入

 

import { Router } from '@angular/router';

 

2.初始化

xport class HomeComponent implements OnInit { constructor(private router: Router) {
}
  ngOnInit() {
  }
goNews(){
// this.router.navigate(['/news', hero.id]);
     this.router.navigate(['/news']);
  }
}

3.路由跳转

 

this.router.navigate(['/news', hero.id]);

 

 


路由 get 传值 js 跳转

 

1. 引入 NavigationExtras

 

import { Router ,NavigationExtras} from '@angular/router';

 

2.定义一个 goNewsContent 方法执行跳转,用 NavigationExtras 配置传参。

 

goNewsContent(){
     let navigationExtras: NavigationExtras = {
       queryParams: { 'session_id': '123' },
       fragment: 'anchor'
};
     this.router.navigate(['/news'],navigationExtras);
  }

 

3.获取 get 传值

 

   constructor(private route: ActivatedRoute) {
     console.log(this.route.queryParams);
}

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/loaderman/p/10912198.html

Angular 动态路由是指在运行时根据某些条件动态生成路由。这种方式可以让我们根据不同的场景动态地加载不同的组件或模块。 具体实现方法如下: 1. 在路由模块声明需要动态生成的路由,但是不需要为它们指定具体的组件或模块。 2. 定义一个服务,该服务负责根据条件生成需要的路由。 3. 在组件注入该服务,并调用服务的方法生成路由。 4. 将生成的路由添加到路由器。 下面是一个示例: 路由模块: ```typescript import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; const routes: Routes = [ { path: 'home', component: HomeComponent }, { path: 'about', component: AboutComponent }, // 动态生成的路由 { path: '', pathMatch: 'full', redirectTo: 'home' }, { path: '**', component: NotFoundComponent } ]; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule { } ``` 动态路由服务: ```typescript import { Injectable } from '@angular/core'; import { Route } from '@angular/router'; @Injectable({ providedIn: 'root' }) export class DynamicRoutesService { constructor() { } generateRoutes(): Route[] { const routes: Route[] = [ { path: 'product', loadChildren: () => import('./product/product.module').then(m => m.ProductModule) } ]; return routes; } } ``` 组件: ```typescript import { Component, OnInit } from '@angular/core'; import { Router, Route } from '@angular/router'; import { DynamicRoutesService } from './dynamic-routes.service'; @Component({ selector: 'app-root', template: ` <h1>Dynamic Routes Demo</h1> <button (click)="generateRoutes()">Generate Routes</button> `, }) export class AppComponent implements OnInit { constructor( private router: Router, private dynamicRoutesService: DynamicRoutesService ) { } ngOnInit() { // 初始化路由 this.router.resetConfig([...this.router.config, ...this.dynamicRoutesService.generateRoutes()]); } generateRoutes() { // 动态生成路由 const newRoutes: Route[] = this.dynamicRoutesService.generateRoutes(); // 将生成的路由添加到路由器 this.router.resetConfig([...this.router.config, ...newRoutes]); } } ``` 在上面的示例,我们定义了一个动态路由服务,该服务负责生成 `product` 路由,并将其添加到路由器。在组件注入该服务,并在 `ngOnInit` 生命周期钩子调用服务的方法生成路由。然后调用 `router.resetConfig()` 方法将生成的路由添加到路由器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值