简体   繁体   English

PlayScalaJS,引导程序无法正常工作

[英]PlayScalaJS with bootstrap not working

I recently came across the fact that the imports of bootstrap were not accessible in my code. 我最近发现在我的代码中无法访问引导程序的导入。 Namely, the plugin alert for jQuery was not available within my code ( jQuery.fn.alert was undefined). 也就是说,我的代码中没有jQuery的插件警报( jQuery.fn.alert未定义)。 I had in the header: 我在标题中:

<script src="@routes.Assets.at("lib/jquery/jquery.min.js")" 
  type="text/javascript"></script>

<script src="@routes.Assets.at("lib/bootstrap/js/bootstrap.min.js")" 
  type="text/javascript"></script>

and in the body: 在身体里:

<body>
    @playscalajs.html.scripts("client")
</body>

In the client script, when it called $("...").alert() , I receive the error : alert is not a function , although I verified bootstrap did define it. 在客户端脚本中,当它调用$("...").alert() ,我收到错误alert is not a function ,虽然我验证了bootstrap确实定义了它。

How to make bootstrap and playscalajs work nicely together? 如何让bootstrap和playscalajs很好地协同工作?

The problem came from @playscalajs.html.scripts("client") which is unfold to these: 问题来自@playscalajs.html.scripts("client") ,它们展现在以下方面:

<script src="/assets/client-jsdeps.js" type="text/javascript"></script>
<script src="/assets/client-fastopt.js" type="text/javascript"></script>
<script src="/assets/client-launcher.js" type="text/javascript"></script>

jQuery is imported again in client-jsdeps.js since it is a scala-js dependency. jQueryclient-jsdeps.js再次导入,因为它是scala-js依赖项。 Since I could not interleave bootstrap imports before the client-launcher.js script and after the client-jsdeps.js scripts, I delayed the execution of the main object using the following code: 由于我无法在client-launcher.js脚本之前和client-jsdeps.js脚本之间交错引导导入,因此我使用以下代码延迟了主对象的执行:

// New object calling the previous Main object
@JSExport
object MainDelayed extends js.JSApp { 
  @JSExport def main(): Unit = $(document).ready(Main.main _)
}

// The original object
object Main { 
  def main() = { ... }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM