簡體   English   中英

SyntaxError: 不能在模塊外使用 import 語句——React

[英]SyntaxError: Cannot use import statement outside a module — React

我正在嘗試從該 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")
    },
};

任何幫助表示贊賞。

首先解決您的跨域問題您還應該修復跨域問題 嘗試像這樣使用

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>

    )
}


}}

您可以使用

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

轉換成

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM