javascript - 尝试导入错误: 'ApiRequests' is not exported from './ApiRequests'

标签 javascript reactjs

我是 js 和 React 新手,所以:

我尝试导出js函数:

应用程序.js:

import React from 'react';
import logo from './logo.svg';
import './App.css';
import { render } from 'react-dom';
import { LazyLog } from 'react-lazylog';    
import 'bootstrap/dist/css/bootstrap.min.css';
import { Dropdown  } from 'react-bootstrap';
import {ApiRequests} from './ApiRequests'


var textConst="";
var lines=ApiRequests.GetMockLogLines();
textConst=lines;

和 ApiRequests.js:

export function GetMockLogLines()
{
 let logs=[""]
 return logs;
}

所以,当我编译 js 时(天哪)它让我崩溃了:

Attempted import error: 'ApiRequests' is not exported from './ApiRequests'.

当我将其更改为:

 import ApiRequests from './ApiRequests'

->

 Attempted import error: './ApiRequests' does not contain a default export (imported as 'ApiRequests').  

所以,我只想从另一个文件中获取函数。我做错了什么?

{} 和没有它有什么区别? (您可以给我发送一个开始阅读的链接,请)。

最佳答案

您的文件中没有名为 Apirequests 的函数。您要导出的函数是 GetMockLogLines()

所以你的导入应该是

import {GetMockLogLines} from './ApiRequests'

至于 {} 和不带大括号之间的导入差异在于如何从文件中导出内容。有两种类型的导出 - 默认导出和命名导出。每个文件只能有一个默认导出,并且必须在不带花括号的情况下导入。

您可以在此处阅读更多信息 - https://medium.com/@etherealm/named-export-vs-default-export-in-es6-affb483a0910

关于javascript - 尝试导入错误: 'ApiRequests' is not exported from './ApiRequests' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58802552/

相关文章:

javascript - 如何在uib-datepicker中设置maxdate和mindate

javascript - 直接函数调用和闭包中的 call() apply() 有什么区别

reactjs - AWS 因 aws-exports 导致部署失败加剧

reactjs - 如何等待一个 Action 完成?

reactjs - 在存储数据时,React 中的对象哈希和数组有什么区别?

javascript - React-Bootstrap 组件文档

javascript - Bootstrap Tour Prev 按钮不会转到上一页

javascript - $.text( ["someText"]) - 这是什么意思?

javascript - JavaScript 中的跨浏览器 XPath 实现

reactjs - 在 React-Redux 中转换 API 数据 : Action or Reducer