【问题标题】:How to get params from url in angular and express?如何以角度和表达方式从url获取参数?
【发布时间】:2021-10-14 23:16:26
【问题描述】:

我有一个类似于http://www.example.com/idf34he8sf/9iad2hf7usnf 的网址。我想获取参数idf34he8sf9iad2hf7usnf

我用过下面的代码

角度

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

在节点中

req.params

req.originalUrl

我想获取参数idf34he8sf9iad2hf7usnf

【问题讨论】:

标签: node.js angular express


【解决方案1】:

你必须在你的 api 中定义参数 as-

example/:organizationId/:embedId

然后使用-获取这些参数-

constructor(private router: Router) {
  this.organizationId = this.router.snapshots.params['organizationId']
  this.embedId = this.router.snapshots.params['embedId']
}

【讨论】:

    【解决方案2】:

    Node 对 Angular 一无所知,您需要设计您的 API 以获取这些参数并在调用 API 时传递它们。

    【讨论】:

    • 我使用相同的 URL 来独立获取 Angular 和节点中的参数。我可以获得 queryParams 但不能获得单个参数。
    【解决方案3】:

    你可以使用@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('/');
    

    【讨论】:

      【解决方案4】:

      在 Angular 中,您可以使用 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); 
      });
      

      【讨论】:

        【解决方案5】:
            //import the ActivatedRoute to use route.snapshot.params
            
        
            import { ActivatedRoute} from '@angular/router';
        
        
             //define  a variable to store the param from url 
             
             public param:any;         
        
            //And in constructor create a instance of ActivatedRoute
            
        
            constructor(private route:ActivatedRoute,) {}
        
            //Then in ngOnInit
            ngOnInit(): void {
            
              this.param = this.route.snapshot.params["Your-params-name in url)"];
               // params-name means (/:id/)
              //this will store the params from your url to the variable param (public)
            }
            
            
             
            //in my url is I wanted to store specific id of a book
            // from url localhost:4200/books/60ab539f678gfraep/  
            // I used  
            {path: 'books/:id',component: showBookComponent},
            //in my ROUTING MODULE  
            //so I gave above code as 
            public Id:string;
             ngOnInit():void {
            // this.Id = route.snapshot.params['id'];
        
            }
        
        
        
            
        

        【讨论】:

        • 请不要转储代码。请解释您的答案,因为它会帮助其他人。
        • 我编辑了我的代码 Dath Vader 以使任何人都可以理解
        猜你喜欢
        • 2020-09-11
        • 2018-04-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-13
        相关资源
        最近更新 更多