如何从url中获取参数并进行表达?

时间:2019-07-08 04:56:14

标签: node.js angular express

我有一个看起来像http://www.example.com/idf34he8sf/9iad2hf7usnf的URL。我想获取参数idf34he8sf9iad2hf7usnf

我使用了以下代码

成角度

this.route.paramMap.subscribe(params => {
      this.organizationId = params.get("organizationId");
      this.embedId= params.get("embedId"); 
}

在节点中

req.params

req.originalUrl

我想获取参数idf34he8sf9iad2hf7usnf

5 个答案:

答案 0 :(得分:0)

Node对Angular一无所知,您将需要设计API以接受那些参数并在调用API时将其传递。

答案 1 :(得分:0)

您可以使用@ angular / router

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

定义一个变量以保存URL

 href:string;
params:string[];

然后在构造函数中

 constructor(private router: Router) {

}

在ngOnInit

this.href = this.router.url;
    this.params = this.href.split('/');

答案 2 :(得分:0)

在角度模式下,您可以使用ActivatedRoute从URL获取参数

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

constructor(route: ActivatedRoute){}

/* Do Something*/

public someFunction(){
    this.route.queryParams.subscribe(params => {
             /* use Params*/   
    }
}

在NodeJS中,您需要在声明路由的同时进行添加,如下所示:

router.get('/:id', (req,res) =>{
   let id = req.params; // for parameterised routes
   // fetching query string  use below:
   // region = req.query.region;
   return res.json(id); 
});

答案 3 :(得分:0)

您必须在api中将参数定义为-

example/:organizationId/:embedId

然后使用-

获取这些参数
constructor(private router: Router) {
  this.organizationId = this.router.snapshots.params['organizationId']
  this.embedId = this.router.snapshots.params['embedId']
}

答案 4 :(得分:0)

    //import the ActivatedRoute to use route.snapshot.params
    

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


     //define  a variable to store the param from url 
     
     Id:string;         

    //And in constructor create a instance of ActivatedRoute
    

    constructor(private route:ActivatedRoute) {}

    //Then in ngOnInit
    ngOnInit(): void {
    
      this.Id = this.route.snapshot.params["Your-params-name in url)"];
       // params-name means (/:id/)
      //this will store the params from url to your variable Id
    }
    
    
     
    //in my url is i wanted to store specific id of a book
    // from url /books/60ab539f678gfraep/  
    // i used books/:id in my routing module so i give 
    // above code as 
    
     ngOnInit():void {
    // this.Id = route.snapshot.params['id'];

    }



    

    /