javascript - 在mvc中根据dev/prod环境加载index.html中不同的脚本

标签 javascript asp.net-mvc angular systemjs

我正在尝试根据我使用的环境加载不同的 javascript 文件。我创建了一个 Angular2 应用程序,它位于 MVC 应用程序内。

在我的index.html (_layout.cshtml) 中,我有以下脚本:

<script src="~/public/lib/js/vendors.min.js"></script> <!--bundled dependencies-->
@*<script src="~/public/dist/js/app.min.js"></script> <!--bundled source-->*@

<script src="~/system.dev.config.js"></script>    
<script>
    System.import('../public/dist/AppComponent/main.js').catch(function (err) { console.error(err); });
</script>

此代码将加载 Angular2 依赖项并启动应用程序。我想区分开发(当我在应用程序上开发时)和在线生产时。

我想区分,所以只加载以下文件:

开发环境:

<script src="~/public/lib/js/vendors.min.js"></script> <!--bundled dependencies-->

<script src="~/system.dev.config.js"></script>    
<script>
    System.import('../public/dist/AppComponent/main.js').catch(function (err) { console.error(err); });
</script>

产品环境:

<script src="~/public/lib/js/vendors.min.js"></script> <!--bundled dependencies-->
<script src="~/public/dist/js/app.min.js"></script> <!--bundled source-->

是否有任何选项,因此当我处于 Debug模式时,它应该使用开发脚本,而在 Release模式下则应使用相反的方式?

提前致谢。

最佳答案

您可以使用

@{ 
    #if DEBUG
        <script src="~/public/lib/js/vendors.min.js"></script> <!--bundled dependencies-->

        <script src="~/system.dev.config.js"></script>    
        <script>
            System.import('../public/dist/AppComponent/main.js').catch(function (err) { console.error(err); });
        </script>
    #else
        <script src="~/public/lib/js/vendors.min.js"></script> <!--bundled dependencies-->
        <script src="~/public/dist/js/app.min.js"></script> <!--bundled source-->
    #endif
}

希望对你有帮助

关于javascript - 在mvc中根据dev/prod环境加载index.html中不同的脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40832718/

相关文章:

javascript - React 组件事件处理程序 - 无法访问事件

javascript - 如何在<output>标签中进行更多操作?

css - 为什么我的导航栏的当前页面不突出显示?

asp.net-mvc - MVC4 SelectList 未选择默认对象

javascript - JavaScript 中的错误异常。 TypeError 调用不是一个函数

c# - 我可以从 null 中分辨出未定义的 Action 参数吗?

angular - 将按钮添加到 mat-tab-group

与全局供应商进行 Angular 2 测试

angular - Protractor 和 cucumber : function timed out using async/await

javascript - 使用 CSS 或 JavaScript 更改一次背景图像