网址查询字符串,均值堆栈

时间:2018-10-03 05:25:43

标签: parameters stack angular5 mean query-parameters

我有一个具有平均堆栈的应用程序,如何使我的网址像这样?

skill / 1?role = 123&role = 456

服务

rmdir <directory> /s

组件

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders,HttpParams } from '@angular/common/http';
import { Observable } from "rxjs";
import { GLOBAL } from '../global.service';

 @Injectable({
   providedIn: 'root'
})
export class ProfesionalService {
  public url: string;

constructor(private _http: HttpClient){
    this.url = GLOBAL.url;
  }
getProfe(token, role):Observable<any>{

   let headers = new HttpHeaders().set('Content-Type','application/json')
               .set('Authorization',token);

  return this._http.get(this.url+'ads/professsional/1?role='  + role,  
{headers: headers});
 }
 }

路由

getPro(){
this._route.params.forEach((params: Params) => {
  let role = params['role'];

this._professionalservice.getProfe(this.token,role).subscribe(
    response => {console.log(response.professionals)
        if(!response.professionals){
            this._router.navigate(['/']);
        }else{
           this.professionals=response.professionals;

        }

    },
    error => {
        var errorMessage = <any>error;
        console.log(errorMessage);
        if(errorMessage != null){
            this.status = 'error';
        }
    }
);}  
);
} 

仅对网址有效

技能/ 1?角色= 123

有人可以帮我还是告诉我在哪里可以找到信息?

1 个答案:

答案 0 :(得分:0)

看看下面的这段代码是否对您有帮助。据了解,您想为后端网址动态多次添加相同的queryParam

我假设您将roles参数传递为包含[123,456,789]的数组

getProfe(token, roles : Array<number>):Observable<any>{
   let headers = new HttpHeaders().set('Content-Type','application/json')
               .set('Authorization',token);

    let url = this.url+'ads/professsional/1';

    if(roles.length > 0) {
        url += '?';  // concat '?'
        roles.forEach((r) => 
          {
              url += 'role=' + r + '&';         // add multiple params like role='123'
          }
        )
        url.splice(url.length-1, 1); // remove trailing &
    }   

  return this._http.get(url, {headers: headers});
}