如何在 node.js 中包含默认模型作为参考名称?

How can I include default model as reference name in node.js?

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

export default students

我需要用引用名导入这个

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

但是它给我的错误是没有定义 studentModel,

如果我用户 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";

Exported object will contain property students and when imported 重命名为 studentModel.

选项 2

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

Exported object will contain property students and when imported 重命名为 studentModel.

选项 3

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

因为 students 本身就是 exported as default the exported object itself is students. You could directly rename the import 任何你想要的东西。