我想在 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 硬编码为:
,那么 DO 就可以工作:url: 'books/' + '5f914f39342ba5b506cb386f',
调用我执行的操作:
const bookId = this.props.navigation.getParam('bookId')
console.log(bookId); // The bookId is NOT NULL. Is CORRECT!
getChatRoom(bookId);
我的错误在哪里?
谢谢
最佳答案
使用反引号而不是单引号更改 URL,如下所示
url: `books/${bookId}`
bookId 是变量。要在字符串中添加变量,您必须使用反引号并将变量放入 ${variable_name}
关于react-native - 使用 axios 使用 Rest api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64498431/