这段代码是我使用的解析对象拼接成url字符串的方法
encodeParamsStr: function(obj) {
var params = [];
Object.keys(obj).forEach(function(key) {
var value = obj[key];
// 如果值为undefined我们将其置空
if (typeof value === "undefined") {
value = "";
}
// 对于需要编码的文本(比如说中文)我们要进行编码
params.push([key, encodeURIComponent(value)].join("="));
});
return params.join("&");
},
下面这个是我的调用
let baseUrl = 'static/html/charting_library/static/tv-chart.e816a7a6edc9de3ed709.html';
let obj = {
localserver:1,
widgetbar:{"datawindow":false,"details":false,"watchlist":false,"watchlist_settings":{"default_symbols":[]}},
drawingsAccess:{"type":"black","tools":[{"name":"Regression Trend"}]},
timeFrames:[
{"text":"5Y","resolution":"W","description":"5年","title":"5年"},
{"text":"1Y","resolution":"D","description":"1年","title":"1年"},
{"text":"6M","resolution":"120","description":"6月","title":"6月"},
{"text":"3M","resolution":"60","description":"3月","title":"3月"},
{"text":"1M","resolution":"30","description":"1月","title":"1月"},
{"text":"5D","resolution":"5","description":"5日","title":"5日"},
{"text":"1D","resolution":"1","description":"1日","title":"1日"}
],
locale:'zh',
customCSS:'night.css',
debug:false,
timezone:'Asia/Shanghai',
}
this.chartUrl = `${baseUrl}#${this.$utils.encodeParamsStr(obj)}`;
$("#chart_iframe").attr('src', this.chartUrl);
下图这个是访问的url,其中对象并没有被解析出来,还是Object,我需要的正确效果应该是像图二这样被编译
慕虎7371278
慕勒3428872
相关分类