想象一个简单的 Play Action 定义为
def reactTest = Action {
request => Ok(views.html.hello("JOHN"))
}
而hello.scala.html
这个样子的,使用基本React.js例如:
@(name: String)
....
<div id="example"></div>
<script type="text/jsx">
React.render(
<h1>Hello, @name!</h1>, <---- NAME PARAMETER USED HERE
document.getElementById('example')
);
</script>
这工作正常,结果将是“你好,约翰!” 页。现在,我知道 Scala 代码在服务器上执行,JS 代码在客户端执行,但我想知道是否有办法将@name
参数传递给相同的 javascript (jsx) 代码,如果这样的代码在单独的.js 文件,<div>
看起来像:
<div id="example"></div>
<script type="text/jsx" src="@routes.Assets.at("javascripts/hello.js"></script>
有没有办法将@name
参数传递给脚本hello.js
?