简体   繁体   English

导入JavaScript吗?

[英]Imports in JavaScript?

I found a tutorial for SAPUI5 with XML. 我找到了带有XML的SAPUI5教程。 So I got now a index.html, a mainView.view.xml and a mainView.controller.js 所以我现在有了一个index.html,一个mainView.view.xml和一个mainView.controller.js

In the controller I got a method for handling a button click. 在控制器中,我有一种用于处理按钮单击的方法。

sap.m.MessageToast.show("Hello");

But that it works I need to write "sap.m. [...]" everytime I want to call a method. 但这确实有效,每次我想调用方法时,我都需要写“ sap.m. [...]”。 Isn't there a way to import the sap.m lib so I can just write MessageToast.show ? 没有办法导入sap.m库,所以我可以只写MessageToast.show吗?

The current sapui5 documentation propagates the Asynchronous Module Definition (AMD) pattern for your javascript classes. 当前的sapui5文档为您的javascript类传播了异步模块定义 (AMD)模式。

There you have to import all the needed classes explicitly one by one. 在那里,您必须一个接一个地显式导入所有需要的类。 But afterwards you can use them without namespace: MessageToast.show("Hello"); 但是之后,您可以不带名称空间使用它们: MessageToast.show("Hello"); .

The advantage of the AMD pattern is that the needed resources can be loaded asynchronously from the server. AMD模式的优点是可以从服务器异步加载所需的资源。


If you don't want to use AMD, RCs comment to your question is the way to go: var MessageToast = sap.m.MessageToast; 如果您不想使用AMD,则RC对您的问题的评论是解决方法: var MessageToast = sap.m.MessageToast; or you could do something like var m = sap.m; m.MessageToast.show("hello"); 或者您可以执行类似var m = sap.m; m.MessageToast.show("hello"); var m = sap.m; m.MessageToast.show("hello"); but i would not recommend that. 但我不建议这样做。

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

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