请考虑以下数据定义文件:
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
函数正常工作?