简体   繁体   English

如何将自定义包导入Scala Play! 框架2.0

[英]How to import a custom package into Scala Play! Framework 2.0

I'm working in Play! 我在Play工作! Framework 2.0 with Scala on sublime text editor. 带有Scala的Framework 2.0在sublime文本编辑器上。 I wanted to create some custom helpers which are just a few methods relating to the controller or goal. 我想创建一些自定义助手,这只是与控制器或目标相关的几种方法。 So I created a folder in the "app" directory called "helpers," for example I have a helper called SiteHelper.scala 所以我在“app”目录中创建了一个名为“helpers”的文件夹,例如我有一个名为SiteHelper.scala的帮助器

in /app/helpers/SiteHelper.scala I start with 在/app/helpers/SiteHelper.scala我开始

package helpers

class SiteHelper {

   def method() = {}

}

Now in my controller I want to be able to do this: import helpers.SiteHelper 现在在我的控制器中我希望能够这样做: import helpers.SiteHelper

then use the method in my controller: SiteHelper.method() 然后在我的控制器中使用该方法: SiteHelper.method()

When I try this I get a compilation error: "not found: value SiteHelper" 当我尝试这个时,我得到一个编译错误:“找不到:值SiteHelper”

How can I use my helper classes in my controllers? 如何在控制器中使用我的助手类?

It seems you want to use an object instead of a class 看起来你想要使用一个对象而不是一个类

package helpers

object SiteHelper {

 def method() = {}
}

Then in you constroller: 然后在你的constroller:

import helpers.SiteHelper

object MyConstroller {
  SiteHelper.method()
}

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

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