我想在react native中使用一个带有axios的rest api。我想得到一个特定的项目,所以URL如下/book/637777378我如何用axios传递该数字id。
到目前为止我所做的:
import axios from 'axios';
import config from '../Config'
const requestHelper = axios.create({
baseURL: config.prodBaseUrl,
});
export default {
books: {
getAll: () => requestHelper({
url: 'books',
method: 'get',
}),
getOne: bookId => requestHelper({
url: 'books/' + bookId, // '5f914f39342ba5b506cb386f',
method: 'get',
}),
create: data => requestHelper({
url: 'books',
method: 'post',
data,
}),
},
};
代码不起作用。但如果我把书的id硬编码为:,确实有效
url: 'books/' + '5f914f39342ba5b506cb386f',
调用我所做的操作:
const bookId = this.props.navigation.getParam('bookId')
console.log(bookId); // The bookId is NOT NULL. Is CORRECT!
getChatRoom(bookId);
我的错误在哪里?
感谢
使用backticks而不是单引号按如下方式更改URL
url: `books/${bookId}`
bookId是变量。要在字符串中添加变量,必须使用backticks并将变量放入${variable_name}中