CRA中具有可变路径的动态导入

时间:2020-05-16 01:00:52

标签: javascript reactjs create-react-app

任何人都知道为什么您不能在create react应用程序中使用带有动态导入的模板文字 下面的方法行不通,但是如果我只是将路径作为字符串正确放置,它将起作用。

示例:这将引发错误未捕获(承诺)错误:找不到模块'../ components / test / page1 / data'

useEffect(() => {
    const folder = "test";
    const id = "page1";
    const path = `../components/${folder}/${id}/data`;
    import(path).then((res) => {
      console.log("module = ", res.default);
    });
  }, []);

工程

useEffect(() => {
    import("../components/test/page1/data").then((res) => {
      console.log("module = ", res.default);
    });
  }, []);

0 个答案:

没有答案