依赖项中的“./has!dom-addeventlistener?:./aspect”是什么意思?

What is the meaning of "./has!dom-addeventlistener?:./aspect" in a dependency?

我在项目中使用 dojo toolkit,它使用 require.js 加载脚本。 在 on.js 中,它加载以下脚本:

define(["./has!dom-addeventlistener?:./aspect", "./_base/kernel", "./sniff"], function(aspect, dojo, has){

使用 on.js 时出现未找到错误:

Uncaught Error: 
  Missing: http://localhost:8585/dom-addeventlistener?:./aspect.js

我想知道 ./has!dom-addeventlistener?:./aspect 符号有什么作用,如果我删除它,潜在的影响是什么。当我只做 ./aspect 时它似乎有效。

谢谢!

当使用 dojo/has 模块作为插件时,它的行为类似于三元运算符:

The conditional (ternary) operator is the only JavaScript operator that takes three operands. This operator is frequently used as a shortcut for the if statement.

condition ? expr1 : expr2 

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

最好通过参考中的以下示例进行解释:

require(["dojo/has", "dojo/has!touch?dojo/touch:dojo/mouse", "dojo/dom", "dojo/domReady!"],
    function(has, hid, dom){
        if(has("touch")){
            dom.byId("output").innerHTML = 
                "You have a touch capable device and so I loaded <code>dojo/touch</code>.";
        } else {
            dom.byId("output").innerHTML =
                "You do not have a touch capable device and so I loaded <code>dojo/mouse</code>.";
        }
    }
);

https://dojotoolkit.org/reference-guide/1.10/dojo/has.html

重要的一点:dojo/has!touch?dojo/touch:dojo/mouse 这里发生的是,dojo/has 模块被加载并检查 touch(如果你有一个支持触摸的设备),如果测试正常结果发现,它加载了 dojo/touch。如果不是,它会加载 dojo/mouse.

在您的情况下:./has!dom-addeventlistener?:./aspect"dojo/has 测试 DOM 的 addEventListener 方法的可用性。如果找到它,则不会加载任何内容,因为 ?: 之间的部分被省略了。如果未找到,它会加载 dojo/aspect

如果一个模块也可以在服务器端 运行 而在浏览器中没有 DOM ,那么通常使用这样的代码。我会说如果你只在浏览器中 运行 你可以安全地忽略整个要求,因为每个像样的浏览器都支持 addEventListener

就是说,您遇到的错误是 dojo/has 相对模块路径无法正确加载。它也应该在加载 ./aspect 时失败,但它不会失败,这很奇怪。确保 dojo/has 可用,因为删除要求是一种解决方法,而不是解决实际问题的方法。