如何加载相对于定义模块的依赖项 [英] How to Load Dependencies Relative to Defining Module

查看:36
本文介绍了如何加载相对于定义模块的依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个库项目,所以我可以在应用程序之间共享一个格式化程序和一些自定义控件.这很有效,它只是将库添加到 neo-app.json 文件中.现在我可以加载我的通用格式化程序:

I made a library project so I can share a formatter and some custom controls between applications. This works pretty well, it's just adding the library to the neo-app.json file. Now I can load my common formatter:

app/neo-app.json

{
    "path": "/resources/shared/",
    "target": {
        "type": "application",
        "name": "mysharedlibrary",
        "entryPath": "/"
    },
    "description": "My shared library"
}

应用程序/控制器

sap.ui.define([
    "shared/formatters"
], function(formatters) {
//etc

然后我想添加一个共享的基本控制器,它已经设置了格式化程序.所以我进入我的共享库项目并添加一个控制器,然后添加格式化程序:

Then I wanted to add a shared base controller, which already has the formatters set. So I go into my shared library project and add a controller, then add the formatter:

mysharedlibrary/SharedController

sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "formatters" /* <----- this is the problem */
], function(Controller, formatter) {
    "use strict";

    return Controller.extend("shared.SharedController", {
        formatter: formatter
    });
});

然后在我的应用中使用它:

And then use it in my app:

app/Controller.js

// fails. Shared base controller can't load formatter. 
sap.ui.define([
    "shared/SharedController"
], function(Controller) {
    "use strict";

    return Controller.extend("my.app.controller.App", {

    });
});

这种方法行不通.实现此控制器的应用程序可以找到控制器,但随后将格式化程序加载到共享控制器似乎失败,因为找不到它.我不确定如何让共享控制器加载文件相对于它自己的路径而不是相对于实现应用程序的路径.

This approach doesn't work. The app implementing this controller can find the controller, but then loading the formatter into the shared controller seems to fail because it can't be found. I'm not sure how to make the Shared Controller load files relative to it's own path instead of relative to the implementing application's path.

我在 SharedController 文件中放什么来加载格式化程序?

What do I put in the SharedController file to load the formatters?

推荐答案

sap.ui.define 支持声明相对于模块自身路径的依赖关系.

sap.ui.define supports declaring dependencies relative to the module's own path.

为方便起见,依赖项的名称可以以 './' 段开头,该段将替换为包含当前定义的模块的包的名称(相对名称).

As a convenience, the name of a dependency can start with the segment './' which will be replaced by the name of the package that contains the currently defined module (relative name).

最佳做法是[...] 尽可能为依赖项使用相对名称.

It is best practice to [...] use relative names for the dependencies whenever possible.

在上面的例子中,如果 SharedController.jsformatters.js 存储在同一个文件夹中,控制器的依赖将如下所示:

In the case above, if SharedController.js and formatters.js are stored in the same folder, the dependency of the controller would look like this:

sap.ui.define([
  "sap/ui/core/mvc/Controller",
  "./formatters"
], function(Controller, formatter) {
  "use strict";

  return Controller.extend("shared.SharedController", {
    formatter: formatter
  });
});

如果它在上层目录,那么像这样:"../formatters"

If it's in upper directory, then like this: "../formatters", etc.

这篇关于如何加载相对于定义模块的依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆