UMD/AMD 解决方案
对于那些通过UMD进行并通过编译的人来说require.js
,有一个简洁的解决方案。
在需要tether
作为依赖项的模块中,作为 UMD 加载Tooltip
,在模块定义之前,只需在 Tether 的定义上放置简短的片段:
// First load the UMD module dependency and attach to global scope
require(['tether'], function(Tether) {
// @todo: make it properly when boostrap will fix loading of UMD, instead of using globals
window.Tether = Tether; // attach to global scope
});
// then goes your regular module definition
define([
'jquery',
'tooltip',
'popover'
], function($, Tooltip, Popover){
"use strict";
//...
/*
by this time, you'll have window.Tether global variable defined,
and UMD module Tooltip will not throw the exception
*/
//...
});
最开始的这个简短片段实际上可以放在应用程序的任何更高级别,最重要的是 - 在实际使用具有依赖关系的bootstrap
组件之前调用它。Tether
// ===== file: tetherWrapper.js =====
require(['./tether'], function(Tether) {
window.Tether = Tether; // attach to global scope
// it's important to have this, to keep original module definition approach
return Tether;
});
// ===== your MAIN configuration file, and dependencies definition =====
paths: {
jquery: '/vendor/jquery',
// tether: '/vendor/tether'
tether: '/vendor/tetherWrapper' // @todo original Tether is replaced with our wrapper around original
// ...
},
shim: {
'bootstrap': ['tether', 'jquery']
}
UPD:在 Boostrap 4.1 Stable 中,他们用Popper.js替换了Tether,请参阅使用文档。