动态参数路由

时间:2020-06-23 01:22:48

标签: angular angular-ui-router

如何在Angular 9中向路线添加随机参数?说我有以下内容:

const appRoutes: Routes = [

  { path: 'heroes',        component: HeroListComponent },
  { path: '',   redirectTo: '/heroes', pathMatch: 'full' }
];

当有人请求\ heroes时,我想附加一个随机生成的整数,因此它变为\ heroes \ 1,heroes \ 8,依此类推。另外,我将需要仅在具有GET请求的英雄路线上进行此操作。

谢谢。

编辑:当我打/ heroes时,下面的console.logs都不起作用。为什么?

import { Observable } from 'rxjs';
import { HttpRequest, HttpInterceptor, HttpHandler, HttpEvent, HttpBackend } 
from '@angular/common/http';
import { Injectable } from '@angular/core';

@Injectable()
export class AuthInterceptorService implements HttpInterceptor {

intercept(req: HttpRequest<any>, next: HttpHandler) : Observable<HttpEvent<any>> {
  
  if ( req.method === 'GET' ) {
    console.log("did it work?");
  }

  console.log("here in auth-interceptor.service ...");

  req = req.clone({ setHeaders : { 
                      'Cache-Control': 'no-cache',
                      'Pragma' : 'no-cache'
                    } });
  return next.handle(req);
 }
}

0 个答案:

没有答案