vue 动态引入 js 文件

发布时间 2023-10-18 17:34:32作者: 法子

在目录 比如utils/options/ 下面建不同名字的 js,再根据  this.type 变量的值动态引入。(注意 this.type 变量的值要和相对应的 js 文件名一致

  onLoad (options) {
    this.type = options && options.type
    const file = require(`@/utils/options/${this.type}.js`)
    this.typeData = file.default
  }

如果 this.type 变量的值是'normal',就会把 normal.js 引入。是其他的值,就建相应的 js 文件。

normal.js

const collectionModel = {}
export default collectionModel