gpt4 book ai didi

reactjs - 语法错误 : Cannot use import statement outside a module -- React

转载 作者:行者123 更新时间:2023-12-03 08:25:21 26 4
gpt4 key购买 nike

我正在尝试从此 api 中为医院提取信息。我能够使用几乎相同的代码成功地从另一个 api 中提取。找不到解决方案,不断收到“SyntaxError:无法在模块外使用导入语句”。这是我正在使用的代码:

components 文件夹只有这个文件。

    import React from "react";
import getHospitals from "../other/api";

function loadHospitals() {

useEffect(() => {
loadHospitals()
}, []);

HospitalsAPI.getNews()
.then(res => {
console.log(res)
})
.catch(err => console.log(err));
};

export default loadHospitals;

一个单独的文件有这个文件:
import axios from "axios";

export default {
getHospitals: function() {
return axios.get("https://services1.arcgis.com/Hp6G80Pky0om7QvQ/arcgis/rest/services/Urgent_Care_Facilities/FeatureServer/0/query?where=1%3D1&outFields=*&outSR=4326&f=json")
},
};

任何帮助表示赞赏。

最佳答案

首先解决您的跨域问题
You should also fix Cross-Origin  issue
尝试像这样使用

import React from "react";

import axios from "axios";


export default class loadHospitals extends React.Component {


componentDidMount() {
axios
.get("https://services1.arcgis.com/Hp6G80Pky0om7QvQ/arcgis/rest/services/Urgent_Care_Facilities/FeatureServer/0/query?where=1%3D1&outFields=*&outSR=4326&f=json")
.then(res => {
console.log(res);

})
.catch(err => {

});

}

render() {
return (

<h2>
test hospital
</h2>

)
}


}}

关于reactjs - 语法错误 : Cannot use import statement outside a module -- React,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61299492/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com