How to convert JSON to query string in angular2?

前端 未结 4 2003
自闭症患者
自闭症患者 2021-02-05 07:07

I\'m new to Angular2. I have a JSON object, as below:

var options = {
  param1: \"parama1\",
  param2: \"parama2\",
  param3: \"parama3\"
};

wh

4条回答
  •  时光说笑
    2021-02-05 07:38

    How about this:

    ngOnInit(){
        let options = {
          param1: "param1",
          param2: "param2",
          param3: "param3"
        };
    
        let myQuery = 'http://someurl?'
        for (let entry in options) {
            myQuery += entry + '=' + encodeURIComponent(options[entry]) + '&';
        }
    
        // remove last '&'
        myQuery = myQuery.substring(0, myQuery.length-1)
    
        window.location.href = myQuery;
    }
    

    myQuery value is ?param1=param1¶m2=param2¶m3=param3.

提交回复
热议问题