SyntaxError:无法在模块外使用import语句-React

时间:2020-04-19 04:20:04

标签: reactjs api syntax-error

我正在尝试从此api获取医院信息。我能够使用几乎相同的代码成功地从另一个api中提取。无法找出解决方案,请继续获取“ SyntaxError:无法在模块外部使用import语句”。这是我正在使用的代码:

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")
    },
};

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

首先解决跨域问题 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>

    )
}


}}

答案 1 :(得分:0)

您可以使用

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

转换为

import { getHospitals } from "../other/api";