为什么我的导出const函数无法正确导入?

时间:2018-11-13 16:30:43

标签: reactjs

因此,我正在创建一个简单的react应用,该应用可以导出一个文件,该文件包含一个返回对象数组的函数:

export const listStudents = () => {
    return [{name:"Ljuben", lastName:"Angelkoski", indeks:"161161", nasoka:"KNI"},
            {name:"Pere", lastName:"Anastasov", indeks:"17350", nasoka:"PET"},
            {name:"Filip", lastName:"Shabanoski", indeks:"173521", nasoka:"PET"},
            {name:"Martin", lastName:"Krsteski", indeks:"163544", nasoka:"KNI"},
            {name:"Nikola", lastName:"Nacevski", indeks:"154874", nasoka:"ASI"}]; // replace the empty array with actual array with at least 5 student objects
}

在我的主要组件中,我有以下代码:

import React, {Component} from 'react'
import listStudents from './repository/studentRepository.js'

class Main extends Component{
    constructor(props){
        super(props);

        this.state={
            students: listStudents
        }
    }

    render(){
        return (
            <h2></h2>
        )
    }
}

export default Main

但是,当我运行该应用程序时,它给了我这个错误:

./src/Main.js
Attempted import error: './repository/studentRepository.js' does not contain a default export (imported as 'listStudents').

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

listStudents是命名导出,因此您必须这样导入:

import { listStudents } from './repository/studentRepository.js'

如果您不想更改导入,则可以使用默认导出功能。

export default () => {
    return [/* ... */]; 
}

答案 1 :(得分:0)

如果您未在子组件中提及默认导出,则需要通过破坏

来调用该类。

从'./children'导入{listtudents};

或者您必须在

之类的孩子中提及默认导出

导出默认常量列表= {}