In vue, replace the parameters in {} in the url obtained from the interface with the corresponding values through js

Recently, I encountered a requirement in my work. First, configure a click-to-jump link for the button on a page, manually enter the url to be redirected and its required parameters, and then obtain the configured one through the interface on another page. url, and replace the parameters in it, this is the first time to deal with this situation, so record it

The front-end page configuration url, the parameter value is occupied by {} package

Then get the configured url through the interface on the adaptation page and replace the parameters in it

1. First find all parameter arrays by regular matching

2. Then, by traversing the parameter array, remove the outer braces from the array value, and finally replace {FLOWID} in the url with the value corresponding to FLOWID

var array = []
var row={
 FLOWID:'erf343wfdfdas',
 FLOWFORMID:'fjiwoqiuo3829038fashkda'
}
var url='/lc/fiveTuple/testPage?flowId={FLOWID}&formId={FLOWFORMID}'
array = url.match(/{(.*?)}/g) //结果:['{FLOWID}', '{FLOWFORMID}']
if (array && array.length) {
  array.forEach(val => {
     var val1 = val.substring(1, val.length - 1)
     url = url.replace(val, row[val1])
  })
}
console.log(url)  //结果:/lc/fiveTuple/testPage?flowId=erf343wfdfdas&formId=fjiwoqiuo3829038fashkda

Guess you like

Origin blog.csdn.net/WYB_BOOM/article/details/131019355