javascript - 导出和导入对象库时出现问题
问题描述
考虑以下数据定义文件:
Circle.js
export default {
x: 0,
y: 0,
radius: 35
}
Line.js
export default {
x1: 0,
y1: 0,
x2: 0,
y2: 0
}
形状库 ( shapeslib
):
import Circle from "./Circle";
import Line from "./Line";
const shapes = {
Circle ,
Line
};
export const getShape = name => {
if (!shapes[name]) throw new Error("(getShape) Cannot retrieve shape " + name);
return shapes[name];
};
在主程序中,我想使用它们:
import { getShape } from "../shapeslib";
const GetData = () => {
let circle = getShape("Circle"); << Returning error (Cannot retrieve shape Circle)
let line = getShape("Line");
console.log("The circle radius is " + circle.radius);
console.log("The line X1 is " + line.x1);
}
尝试获取形状对象时出现错误(显示在主代码中)...
如何使该getShape
功能正常工作?
解决方案
推荐阅读
- shell - 如何使用 shell 脚本获取 html 文件中的特定值
- html - 使用ajax asp.net从控制器加载带有数据的html页面
- c# - 从服务器端 Blazor 应用调用 API 时,用户声明始终为空
- javascript - vs代码终端控制台不使用javascript
- r - 在 R sommer v 4.1.1 中预测函数
- python - Python 和 xml.etree.ElementTree -
- javascript - 从字典中提取值(在 Node-RED 中)
- html - Spring Boot 应用程序 - thymeleaf 会在一段时间后加载图片。某种缓存问题?
- azure - Azure DevOps Pipeline Build Immutable Image (Packer) 找不到脚本
- memory-leaks - 通知程序的潜在内存泄漏