首页 > 解决方案 > 导出和导入对象库时出现问题

问题描述

考虑以下数据定义文件:

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功能正常工作?

标签: javascriptecmascript-6

解决方案


推荐阅读