我需要通过module.export从axios响应中读取数据。 这是我的数据: http:// localhost:8001
{
"name":"Nama Nih",
"desc":"Sekolah Terpadu Al-Qudwah - Yayasan Islam Qudwatul Ummah",
"prefix":"alqudwah",
"footerText":"Yayasan Islam Qudwatul Ummah | All Rights Reserved 2020",
"logoText":"Al-Qudwah",
"needLogin":false
}
我使用以下代码从axios调用数据: brand.js
var axios = require('axios');
module.exports = axios.get('http://localhost:8001', {
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json'
}})
.then(function (response) {
return response.data;
})
.catch(function (error) {
console.log(error);
})
.then(function () {
});
但是当我读取响应数据时,它与预期的不一样。我尝试这样控制台日志返回值:
这是我在控制台输入代码的地方。记录响应。
import React, { PureComponent } from 'react';
import brand from 'ba-api/brand';
import { Helmet } from 'react-helmet';
class Dashboard extends PureComponent {
render() {
console.log(brand);
const title = brand.name + ' - Dashboard';
const description = brand.desc;
const { classes } = this.props;
return (
<div>
<Helmet>
<title>{title}</title>
<meta name="description" content={description} />
<meta property="og:title" content={title} />
<meta property="og:description" content={description} />
<meta property="twitter:title" content={title} />
<meta property="twitter:description" content={description} />
</Helmet>
</div>
);
}
}
Dashboard.propTypes = {
classes: PropTypes.object.isRequired,
};
export default withStyles(styles)(Dashboard);
如何读取此数据?
答案 0 :(得分:0)
好吧,您不能直接导出获取的数据。从技术上讲,您可以通过以下方式实现类似的目的:
注意:以下不是最佳做法。在React组件中,最好使用suggested
方法。
// brand.js
module.exports = (() => {
const request = new XMLHttpRequest();
request.open('GET', 'http://localhost:8001', false);
request.send(null);
if (request.status === 200) {
return request.responseText;
}
})();
如果您可以生存而不必同步访问所请求的资源,则可以将axios包装在自定义钩子中。
import { useEffect, useState } from 'react';
const useAxios = (url) => {
const [state, setState] = useState({});
useEffect(() => {
axios
.get(url)
.then(({ data }) => setState({ data }))
.catch(error => setState({ error }));
}, []);
return state;
}
// .. in your component
const Component = () => {
const { data, error } = useAxios('http://localhost:8001');
if (error) {
... error component
}
if (!data) {
... loading component
}
return (
... component that is consuming `data`
);
}
在特定情况下,由于仅将这些数据用于设置元数据,因此可以跳过错误并加载组件。
答案 1 :(得分:0)
我已经通过以下解决方案解决了这个问题: brand.js
var axios = require('axios');
const sendGetRequest = async () => {
try {
const resp = await axios.get('http://localhost:8001', {
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json'
}})
.then(function (response) {
return response.data;
});
return resp;
} catch (err) {
console.error(err);
}
};
module.exports = sendGetRequest();
然后我用以下代码调用该函数:
import React, { PureComponent } from 'react';
import brand from 'ba-api/brand';
import { Helmet } from 'react-helmet';
class Dashboard extends PureComponent {
constructor(props){
super(props);
this.state = {
title: '',
description: ''
}
}
componentDidMount(){
brand.then(data => {
this.setState({
title: data.name,
description: data.desc
});
});
}
render() {
const title = this.state.title + ' - Dashboard';
const description = this.state.description;
const { classes } = this.props;
return (
<div>
<Helmet>
<title>{title}</title>
<meta name="description" content={description} />
<meta property="og:title" content={title} />
<meta property="og:description" content={description} />
<meta property="twitter:title" content={title} />
<meta property="twitter:description" content={description} />
</Helmet>
</div>
);
}
}
Dashboard.propTypes = {
classes: PropTypes.object.isRequired,
};
export default withStyles(styles)(Dashboard);
感谢所有回答我问题的人。