如何在视图中的脚本部分中包含脚本包 [英] How to include script bundle in scripts section in view

查看:25
本文介绍了如何在视图中的脚本部分中包含脚本包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 @RenderSection("scripts") 部分的布局,我有一个包需要包含在此部分中以用于某些视图.我认为只在视图中执行此操作会起作用,但它不会呈现脚本.

I have a layout with an @RenderSection("scripts") section and I have a bundle that would need to be included in this section for some views. I thought that just doing this in the view would work, but it's not rendering the scripts.

@section scripts {
    @Scripts.Render("~/bundles/myBundle")  
}

在我看来,如何在脚本部分包含一个包?

布局

@Scripts.Render("~/bundles/jquery", "~/bundles/scripts")
@RenderSection("scripts", required: false)

查看

@section scripts {
    @Scripts.Render("~/bundles/movie")  
}

推荐答案

为什么将渲染部分与捆绑混合使用?如果您选择走捆绑路线,您可以简单地将您的脚本放在 .JS 文件中,如果您愿意,可以将其放在自己的包中,然后在您的视图中调用该包.例如:

Why mix the rendersection with bundling? If you choose to down the route of bundling, you can simply put your scripts in .JS file ,put it in their own bundle if you like and call that bundle on your view. For ex:

     bundles.Add(new ScriptBundle("~/bundles/myscripts").Include(
                    "~/Scripts/myscript1.js",
                    "~/Scripts/myscript2.js")); 

然后视图将有以下内容:

then view will have the following:

    @Scripts.Render("~/bundles/myscripts")   

还要确保您的 Web.config 将编译调试设置为 false,如下所示:

Also make sure your Web.config has compilation debug set to false like below:

  <compilation debug="false" />            

它确保脚本被捆绑和缩小.

it makes sure the scripts are bundled and minified.

更新

根据评论和我最近的经验,我明白为什么我们要同时使用两者.社区学习的完美案例!:) 所以,如果你决定回来重构,我会确保一开始就没有错别字.如果它仍然不起作用,请告诉我问题是什么,我会相应地更新答案.感谢大家!

Based on comments and my recent experience, I can see why would we want to use the two together. Perfect case of learning in the community! :) So, if you decide to come back for refactoring, I would make sure there are no typos to begin with. If it still does not work, please let me know what the problem is and I will update the answer accordingly. Thanks everyone!

这篇关于如何在视图中的脚本部分中包含脚本包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆