javascript - 给url添加参数。使网址动态化

标签 javascript reactjs ecmascript-6

我有一个函数,我试图将 id 参数添加到 url 中,并动态更改 url 并将 id 参数添加到其中。但使用花括号并不能反射(reflect)参数。

submitRating(rate, id) {
  this.setState({
    rating: rate,
  });
  console.log(this.state.rating);
  console.log(id);    ------> the console calls the id eg. id = 3499583
  const url = "https://oversight-ws.herokuapp.com/api/politicians/{id}/rating"  ---> that id should be rendered here on {id}
  console.log(url); ---> i want to return this url https://oversight-ws.herokuapp.com/api/politicians/3499583/rating
  return;

最佳答案

您正在使用 es6,因此您可以使用字符串插值来执行此操作:

const id = 3499583;
const url = `https://oversight-ws.herokuapp.com/api/politicians/${id}/rating`;
console.log(url);

关于javascript - 给url添加参数。使网址动态化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48517926/

相关文章:

javascript - jquery 和 highcharts SCRIPT1014 : Invalid character 出现无效字符错误

javascript - Jquery翻转但不是同一个 child ?

reactjs - 过滤 Flatlist 时未定义不是对象

reactjs - React Route 显示空白页

javascript - 从 node_modules 导入多个类

javascript - 未捕获的类型错误 : Cannot destructure property `name` of 'undefined' or 'null'

javascript - 在 Angular 2 中导入 Pivottable js

javascript - 如何将 Bulma 页面加载器扩展添加为 Vue 组件?

javascript - 如何将 defaultOption 添加到 AsyncSelect?

javascript - 将数组转换为对象,其中数组值为对象键,对象值为空