首页 > 其他分享 >在if判断中使用es6的 import 引入文件无效的解决方案(WebPack)

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

时间:2023-02-22 00:11:07浏览次数:32  
标签:es6 platform mobile modules pc WebPack import store

同一个页面需要通过判断来引入不同的文件时,发现 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");
}

标签:es6,platform,mobile,modules,pc,WebPack,import,store
From: https://www.cnblogs.com/echohye/p/17142988.html

相关文章