1.使用import.meta.globEager()

const getSrc = (name) => {
    const path = `/static/icon/${name}.svg`;
    const modules = import.meta.globEager("/static/icon/*.svg");
    return modules[path].default;
  };

2.在computed属性中使用动态路径

    var imagePath = computed(() => {
      switch (condition.value) {
        case 1:
          const imgUrl = new URL('../assets/1.jpg',
            import.meta.url)
          return imgUrl
          break;
        case 2:
          const imgUrl2 = new URL('../assets/2.jpg',
            import.meta.url)
          return imgUrl2
          break;
        case 3:
          const imgUrl3 = new URL('../assets/3.jpg',
            import.meta.url)
          return imgUrl3
          break;
      }
    });

3.安装插件: plugin-dynamic-import-vars  

        1)npm 安装:

        npm install @rollup/plugin-dynamic-import-vars --save-dev

        2)创建名为 的文件,并添加配置信息:

                

        import dynamicImportVars from '@rollup/plugin-dynamic-import-vars';

        export default {
                plugins: [
                dynamicImportVars({
                        // options
                })
                ]
        };

                //可不写options,默认就行。

        3)实例:                

        function importLocale(locale) {
          return import(`./locales/${locale}.js`);
        }

Logo

前往低代码交流专区

更多推荐