在Play 2.5 HTML模板中自动注入WebJarAssets? [英] Automatically inject WebJarAssets in Play 2.5 HTML template?

查看:112
本文介绍了在Play 2.5 HTML模板中自动注入WebJarAssets?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的custom模块中的Play HTML模板中,我有以下代码行:

In my Play HTML template inside my custom module, I have the following line of code:

<script type="text/javascript" src="@controllers.core.routes.WebJarAssets.at(WebJarAssets.locate("jquery.min.js"))"></script>

这引用了core模块中的WebJarAssets类,如下所示:

This references a WebJarAssets class in the core module that looks like this:

package controllers.core

import javax.inject._
import play.api.http.HttpErrorHandler
import play.api.{Environment, Configuration}

class WebJarAssets @Inject()(errorHandler: HttpErrorHandler, configuration: Configuration, environment: Environment) extends controllers.WebJarAssets(errorHandler, configuration, environment)

请注意,我还在custom模块的build.sbt中包括以下行:

Please note that I have also included the following line in build.sbt in the custom module:

"org.webjars" %% "webjars-play" % "2.5.0",

启动应用程序时,出现以下错误:

When starting the application, I receive the following error:

[error] /Users/john/DemoProject/modules/custom/app/views/custom/templates/main.scala.html:36: not found: value WebJarAssets
[error]     <script type="text/javascript" src="@controllers.core.routes.WebJarAssets.at(WebJarAssets.locate("jquery.min.js"))"></script>
[error]                                                                                  ^

从早期版本中WebJarAssets的实现来看,不需要为此实现并注入自己的控制器(例如,请参见

Judging from the implementation of WebJarAssets in earlier releases, there was no need to implement and inject an own controller for this (for example, see this tutorial).

我做错了什么?我是否甚至需要一个WebJarAssets类(因为我在许多模块中都需要Web jar资产,所以我现在在core模块中添加了它).此外,为什么我需要手动将其注入模板中,为什么不能自动解决?

What am I doing wrong? Do I even need a WebJarAssets class for this (as I need web jar assets in many modules, I added this for now in the core module). Furthermore, why do I need to manually inject it in the template and why is it not automatically resolved?

如果您想知道手动注入对我意味着什么:

In case you wonder what manually inject means for me:

@(title: String, webJarAssets: WebJarAssets)(content: Html)(implicit messages:Messages)
<script type="text/javascript" src="@controllers.core.routes.WebJarAssets.at(webJarAssets.locate("jquery.min.js"))"></script>

推荐答案

webjars-play 2.5.0之前,您可以在模板中使用静态方法.由于Play不再使用全局状态/静态方法,因此已将其从webjars-play中删除,现在您需要注入WebJarAssets.不幸的是,Twirl模板尚不支持注入.因此,您必须将其注入到控制器中,然后将其传递给模板.这是执行此操作的完整示例应用程序: https://github.com/webjars/webjars-播放/树/主/测试项目

Before webjars-play 2.5.0 there was a static method that you could use in your templates. Since Play has been moving away from global state / static methods, that was removed from webjars-play and now you need to inject WebJarAssets. Unfortunately the Twirl templates don't support injection yet. So you have to inject it into your controller and then pass it to your template. Here is a full sample app that does this: https://github.com/webjars/webjars-play/tree/master/test-project

这篇关于在Play 2.5 HTML模板中自动注入WebJarAssets?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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