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

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

问题描述

我用 @RenderSection(脚本)部分的布局,我有一个包,将需要列入本节中的一些的意见。我以为只是在做这个视图会的工作,但它不是渲染脚本。

  @section脚本{
    @ Scripts.Render(〜/包/ myBundle)
}

在我看来,我怎么能包括捆绑到脚本的一节?

布局

  @ Scripts.Render(〜/包/ jQuery的,〜/包/脚本)
@RenderSection(脚本,必需:false)

查看

  @section脚本{
    @ Scripts.Render(〜/包/电影)
}


解决方案

为什么混用捆绑rendersection?如果选择向下​​捆绑的路线,你可以简单地把你的脚本.js文件,把它放在自己的包,如果你喜欢,并调用包在你的看法。对于例如:

  bundles.Add(新ScriptBundle(〜/包/ myscripts)。包括(
                    〜/脚本/ myscript1.js
                    〜/脚本/ myscript2.js));

然后查看将有以下内容:

  @ Scripts.Render(〜/包/ myscripts)

另外,还要确保你的web.config已编译调试设置为false,如下图所示:

 <编译调试=假/>

它确保脚本被捆绑和缩小的。

更新

根据意见,我最近的经验,我可以看到我们为什么要使用两者结合起来。完善的情况下,在社会各界的学习! :)所以,如果你决定回来重构,我会确保有没有错别字开始。如果仍然不能正常工作,请让我知道是什么问题,我会更新相应的答案。感谢大家!

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")  
}

In my view, how can I include a bundle to the scripts section?

Layout

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

View

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

解决方案

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")   

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.

Update

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天全站免登陆