如何在 Orchard CMS 中使用 javascript(js 文件)
谁能告诉我如何在 Orchard CMS 中使用 js 文件?我将其添加到 Layout.cshtml 页面中 Script.include("jquery.js")
。
Can any one tell me please how to use js file in Orchard CMS? I added it to Layout.cshtml page asScript.include("jquery.js")
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你可以双重进行。
第一种方法就像 mdm 所描述的:
IResourceManifestProvider
接口实现(查看 Orchard 源代码 - 它已在许多模块中实现),实现BuildManifests (ResourceManifestBuilder builder)
方法并为给定的 .js 文件创建命名资源@{ Script.Require("[您的资源名称]");在 Razor 视图文件 (.cshtml) 中添加该脚本。
如果您有许多脚本文件,并且它们之间可能存在依赖关系,那么这是首选解决方案。它允许您指定每个脚本文件的依赖关系,并让 Orchard 处理其余的事情(因此,当您引用给定资源时,所有依赖的资源也会以正确的顺序自动引用)。
第二种更简单的方法是直接引用 .cshtml 文件中的 .js 脚本文件,而不创建命名资源。如果您想快速添加对单个脚本的引用,它会很有用。像这样(示例取自 Orchard.Web\Core\Shapes\Views\Document.cshtml):
You can do it twofold.
First approach is just like mdm described:
IResourceManifestProvider
interface (take a look at Orchard source - it's been implemented in many modules), implementBuildManifests(ResourceManifestBuilder builder)
method and create a named resource for a given .js file@{ Script.Require("[your resource name]"); }
in your Razor view file (.cshtml) to include that script.This is a preferred solution if you have many script files, possibly with dependencies between them. It allows you to specify the dependencies for each script file and make Orchard take care of the rest (so when you reference a given resource, all dependent ones would also be automatically referenced in the proper order).
The second, simpler approach is to directly reference the .js script file in your .cshtml file, without creating a named resource. It's useful if you'd like to quickly add a reference to a single script. Like this (example taken from Orchard.Web\Core\Shapes\Views\Document.cshtml):
如果您安装并启用了 Orchard.jQuery 模块,则在视图顶部使用:
查看 Orchard.jQuery 项目中的 ResourceManifest 类(应该位于 Orchard 解决方案文件中的某个位置),您可以在其中看到所有内容您可以使用此语法包含不同的 jQuery 模块(例如
jQueryUI_Core
、jQueryUI_Tabs
等)
If you have the Orchard.jQuery module installed and enabled, then at the top of your view use:
Have a look at the ResourceManifest class in the Orchard.jQuery project (should be somewhere in your Orchard solution file), there you can see all the different jQuery modules you can include using this syntax (e.g.
jQueryUI_Core
,jQueryUI_Tabs
, etc)E.g.
这有点作弊,但您可以将其添加到布局或文档文件中。
It's kinda cheating but you can just add this to the layout or document file.
要获得一个好的示例,请查找模块 ContentPicker。这带有默认的 Orchard 源代码。它展示了如何使用 Script.Require("MyScript")。 JQuery 模块本身就是一个很好的例子。该模块还附带默认源代码。
注:我指的是Orchard 1.7及更高版本
To have a good example look for the module ContentPicker. This comes with default Orchard source code. It shows how you can use Script.Require("MyScript"). JQuery module itself is a good example. This module also comes with default source code.
Note: I am referring to Orchard 1.7 and later versions