Javascript 将参数添加到url。使url动态化
我有一个函数,我正在尝试将id参数添加到url中,并动态更改url并将id参数添加到其中。但是使用大括号并不能反映参数Javascript 将参数添加到url。使url动态化,javascript,reactjs,ecmascript-6,Javascript,Reactjs,Ecmascript 6,我有一个函数,我正在尝试将id参数添加到url中,并动态更改url并将id参数添加到其中。但是使用大括号并不能反映参数 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 =
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;
常量url=`https://oversight-ws.herokuapp.com/api/politicians/${id}/rating`;
console.log(url)代码>您使用的是es6
,因此可以使用字符串插值来执行此操作:
const id=3499583;
常量url=`https://oversight-ws.herokuapp.com/api/politicians/${id}/rating`;
console.log(url)代码>使用ES6
并参照您的要求,这样编写
const id = 3499583;
const url = `https://oversight-ws.herokuapp.com/api/politicians/${id}/rating`;
使用ES6
并参照您的要求,这样编写
const id = 3499583;
const url = `https://oversight-ws.herokuapp.com/api/politicians/${id}/rating`;
您可以使用ES2015模板文本功能
===========================================================================
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;
您可以使用ES2015模板文本功能
===========================================================================
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;
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;