如何在node.js中包含默认模型作为引用名称



我有一个学生模块,我将其导出为默认模块

export default students

我需要导入这个参考名称

我尝试过:import students as studentModel from "../students";

但这给了我一个错误,比如studentModel没有定义,

如果i用户import students from "../students";

那么学生就是工作,任何人都可以指导我错过了什么。

提前感谢

您可以为存储模块的变量命名任何您喜欢的名称。

import studentModel from "../students";
// this is just fine
import students from "../students";
// so is this
import dogsGoToHeaven from "../students";
// and this

选项1

export students;
import {students as studentModel} from "../students";

导出的对象将包含属性students,导入后将重命名为studentModel

选项2

export default {students};
import {students as studentModel} from "../students";

导出的对象将包含属性students,导入后将重命名为studentModel

选项3

export default students;
import studentModel from "../students";

由于students本身被导出为default,因此导出的对象本身是students。您可以直接将导入重命名为您想要的任何内容。

最新更新