在if判断中使用es6的 import 引入文件无效的解决方案(WebPack)

同一个页面需要通过判断来引入不同的文件时,发现 import 写在 if 中这种写法会导致加载不到文件,产生报错;

const platform= localStorage.getItem("platform");
if (platform === "pc") {
  import { pc} from "@/store/modules/pc";
} else {
  import { mobile } from "@/store/modules/mobile";
}

改用 require 方式引入即可

const platform= localStorage.getItem("platform");
let model = null;
if (system === "pc") {
  model = require("@/store/modules/pc");
} else {
  model = require("@/store/modules/mobile");
}
posted @ 2023-02-22 00:09  槑孒  阅读(76)  评论(0编辑  收藏  举报