SAPUI5: How to use external library in controller?

北战南征 提交于 2020-01-25 23:22:38

问题


In my index.html file I have the following code:

<script>
        sap.ui.getCore().attachInit(function() {
            jQuery.sap.registerModulePath("lodash","https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min");
        jQuery.sap.require("lodash");

        new sap.m.Shell({
            app: new sap.ui.core.ComponentContainer({
                height : "100%",
                name : "sap.com.ml.docservicedoc-service-m-learning"
            })
        }).placeAt("content");
    });

How can I now use lodash in one of my controllers (not view)? I tried:

sap.ui.define([
"sap/ui/core/mvc/Controller",
"lodash"], function(Controller, _) {

Unfortunately "_" is always undefined.


回答1:


Figured out a solution:

  1. Create libs folder in your project and put the library into that folder
  2. Inside your controller add the library as the following. Also keep in mind that the value in the define function should be different to the global name of the library:
sap.ui.define([
       "sap/ui/core/mvc/Controller",
       "<project namespace>/libs/lodash/lodash.min",
   ], function(Controller, lodash) {
  1. Use global root variable of library. In case of lodash it is "_"


来源:https://stackoverflow.com/questions/46464218/sapui5-how-to-use-external-library-in-controller

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!