您好,我正在使用 Vaadin 8 和 Vaadin 图表 4.0,并尝试根据用户格式输入和区域设置将图表的数据轴之一格式化为不同的日期时间格式。我尝试注入 moment.js 库并获得一个引用异常,说明 moment 变量不存在。我尝试了 2 种不同的方法,我在下面列出:
(1) 使用@JavaScript注解注入moment.js。
@JavaScript({"https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js", "https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.13/moment-timezone-with-data-2012-2022.min.js"})
@WebServlet(urlPatterns = "/*", name = "IeUIServlet", asyncSupported = true)
@VaadinServletConfiguration(ui = IeUI.class, productionMode = true, heartbeatInterval = 500, closeIdleSessions = false, widgetset = "softcom.ievaadin.AppWidgetSet")
public static class IeUIServlet extends VaadinServlet implements SessionInitListener {//, SessionDestroyListener {
@Override
protected void servletInitialized() throws ServletException {
super.servletInitialized();
// Add any system level init here
//Main.initSystem();
}
@Override
public void destroy() {
super.destroy(); //To change body of generated methods, choose Tools | Templates.
Main.destroy();
}
@Override
public void sessionInit(SessionInitEvent event) throws ServiceException {
event.getSession().getSession().setMaxInactiveInterval(-1);
}
}
还有一个设置格式化程序的地方
YAxis ya = new YAxis();
String js = "function() {";
js += "try {"
+ "var g = moment.tz(\"2014-06-01 12:00\", \"America/New_York\");"
+ "alert(g);"
+ "return g;}"
+ "catch(e) {"
+ "alert(e);}}";
ya.getLabels().setFormatter(js);
(2) 我尝试在函数本身的头部附加脚本标签。
String js = "attempt {"
+ "var script = document.createElement('script');"
+ "script.src = \"https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js\";"
+ "document.head.appendChild(script);"
+ "} catch(e) {"
+ "alert('first one'); }"
+ "try {"
+ "var b = moment().format();"
+ "return b;"
+ "} catch (e) {"
+ "alert(e); }";
JavaScript.getCurrent().execute(attempt);
String js = "function() {";
js += "try {"
+ "var g = moment.tz(\"2014-06-01 12:00\", \"America/New_York\");"
+ "alert(g);"
+ "return g;}"
+ "catch(e) {"
+ "alert(e);}}";
ya.getLabels().setFormatter(js);
任何帮助,将不胜感激。