【问题标题】:How to import js/html files using a switch statement?如何使用 switch 语句导入 js/html 文件?
【发布时间】:2016-12-12 22:38:57
【问题描述】:

我有一个themes.js 文件,它的助手如下:

Template.themes.helpers({
        'allcompanyInformation' : function() {

            var configData = CompanySettings.findOne({"_id" : "dNtMroDWtaKQytD6W"});
            if(configData){
            console.log(configData);
            console.log(configData.generalSettings.storeType);

                 switch(configData.generalSettings.storeType){
                    case 'AutomobileSpares':
                        console.log("AutomobileSpares");
                        import './autospare-inhouse-theme/autospare-inhouse-theme.js';
                        break;

                    case 'FashionJwellery':
                        console.log("FashionJwellery");
                        import './classic-jwellery-themes/classic-jwellery-themes.js';
                        break;

                    case 'PreciousJwellery':
                            if(configData.generalSettings.processType =='Customize'){
                                if(configData.generalSettings.shopType =='inhouse'){
                                console.log(configData.generalSettings.shopType);   
                                console.log("***  In inhouse ***");
                                import '../modules/homepage/homepage.js';
                                import './elegant-jwellery-theme/elegant-jwellery-theme.js';


                            }
                            else{
                                console.log("if not inhouse");
                            }
                        }
                        else{
                            console.log("ProcessType else part");
                        }
                        break;  

                    case 'GroceryStore':
                        import './modern-grocery-theme/modern-grocery-theme.js';
                        break;
                 }
            }


        },
    });

如果满足切换条件,我想导入import '../modules/homepage/homepage.js';。这必须渲染homepage.html,由homepage.js导入。

【问题讨论】:

    标签: javascript meteor meteor-blaze


    【解决方案1】:

    你好,你试过require()而不是import吗?

    根据流星guide,您应将require 用于不在模块顶层的导入。

    例如在你的情况下替代:

    import '../modules/homepage/homepage.js';

    与:

    require('../modules/homepage/homepage.js');

    对所有其他嵌套的 import 语句都这样做。

    【讨论】:

      猜你喜欢
      • 2021-11-18
      • 2021-02-28
      • 2011-08-31
      • 1970-01-01
      • 1970-01-01
      • 2020-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多