【问题标题】:Tooltip Tether with requireJS带有 requireJS 的工具提示系绳
【发布时间】:2016-03-26 08:56:50
【问题描述】:

Bootstrap 的最新测试版 (v4) 使用 Tether js 定位元素,我无法让它在我的 Requirejs 应用程序中工作。

在我的 requirejs 配置中,我有以下垫片

paths: {
    jquery: '/path/to/jquery',
    tether: '/path/to/tether'
},
shim: { 
     'bootstrap': ['tether', 'jquery']       
}

当我想在我的应用中激活工具提示时,我使用以下我认为正确的代码

function page_events() {
    requirejs(['bootstrap'], function(bootstrap) {
        $('[data-toggle="tooltip"]').tooltip(); 
    }); 
}

这应该首先加载引导程序依赖项,然后执行代码。所以通常 Tether 应该包含在这段代码之前。

但是控制台结果是

Uncaught ReferenceError: Tether is not defined

有人有同样的问题吗?

【问题讨论】:

标签: javascript twitter-bootstrap requirejs tether


【解决方案1】:

创建一个这样的脚本:

define(['lib/tether.min'], function(tether) {
    window.Tether = tether;
    return tether;
});

然后改变这个:

paths: {
    jquery: '/path/to/jquery',
    // tether: '/path/to/tether'
    tether: '/path/to/your-own-tether'
},
shim: { 
     'bootstrap': ['tether', 'jquery']       
}

为什么?因为浏览器需要这个:

window.Tether = tether; // May be both requirejs and tether didn't do this

【讨论】:

  • 这里真的需要return tether;吗?
  • 不,window.Tether 使 Tether 在全球范围内可用。如果你不返回任何东西,当你 require 系绳时,tether 参数将是 undefined。所以,这只是为了保持一致性。
【解决方案2】:

如果您希望 Tether 在全球范围内可用,您应该使用脚本标签手动包含它。 RequireJS 不会暴露它。

<script type="text/javascript" src="/js/tether.js"></script>
<script type="text/javascript" src="/js/optimized.min.js"></script>

【讨论】:

  • 这并不是@webmaster 所要求的,因为从问题中可以清楚地看到,他正在使用requireJS,并且您提议将脚本加载为模块依赖项,这打破了整个概念。你可以看看UMD/AMD here的解决方案。
猜你喜欢
  • 1970-01-01
  • 2011-11-03
  • 1970-01-01
  • 1970-01-01
  • 2016-03-19
  • 2023-04-01
  • 2012-11-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多