Javascript 使用typescript使AMD可选
我已经用typescript创建了一个小型的库,我希望能够在我的很多项目中使用它,有些项目使用requirejs,有些项目不使用 我见过其他脚本这样做,它使用Javascript 使用typescript使AMD可选,javascript,module,requirejs,typescript,amd,Javascript,Module,Requirejs,Typescript,Amd,我已经用typescript创建了一个小型的库,我希望能够在我的很多项目中使用它,有些项目使用requirejs,有些项目不使用 我见过其他脚本这样做,它使用define并检查AMD,如果没有,它会将对象附加到窗口对象或其他对象 我想知道最好的方法是什么?如果可能的话,任何快捷方式或w/e都可以在typescript中完成 下面是一个示例模块 export module Utilities { //This is used to grab query string values from a j
define
并检查AMD,如果没有,它会将对象附加到窗口对象或其他对象
我想知道最好的方法是什么?如果可能的话,任何快捷方式或w/e都可以在typescript中完成
下面是一个示例模块
export module Utilities {
//This is used to grab query string values from a javascript file
//pass the filename (without .js) into the constructor, then use
//GetValue(name) to find the query string values
export class QueryStringHelper {
names: string[] = [];
values: string[] = [];
constructor(public fileName: string) {
this.getQueryStringNameAndValues();
}
// GetValue(queryStringName: string) => number;
GetValue(queryStringName: string) {
var i = this.names.indexOf(queryStringName);
if (i == -1)
return undefined;
else {
if (this.values.length > i)
return this.values[i];
}
}
getQueryStringNameAndValues() {
var doc: HTMLDocument = document;
var scriptQuery: string = '';
// Look for the <script> node that loads this script to get its parameters.
// This starts looking at the end instead of just considering the last
// because deferred and async scripts run out of order.
// If the script is loaded twice, then this will run in reverse order.
// take from Google Prettify
for (var scripts = doc.scripts, i = scripts.length; --i >= 0;) {
var script = <HTMLScriptElement>scripts[i];
var match = script.src.match("^[^?#]*\\/" + this.fileName + "\\.js(\\?[^#]*)?(?:#.*)?$");
if (match) {
scriptQuery = match[1] || '';
// Remove the script from the DOM so that multiple runs at least run
// multiple times even if parameter sets are interpreted in reverse
// order.
script.parentNode.removeChild(script);
break;
}
}
var that = this;
scriptQuery.replace(
/[?&]([^&=]+)=([^&]+)/g,
function (_, name, value) {
value = decodeURIComponent(value);
name = decodeURIComponent(name);
that.names.push(name);
that.values.push(value);
return "";
});
}
}
}
这就产生了这个javascript
var TestClass = (function () {
function TestClass(testValue) {
this.testValue = testValue;
}
TestClass.prototype.getTestValue = function () {
return this.testValue;
};
return TestClass;
})();
if (typeof window['define'] === "function" && window['define']['amd']) {
window['define']("TestClass", [], function () {
return TestClass;
});
}
这不是TypeScript支持的模式。原因是这种技术只适用于本身没有任何模块依赖关系的模块。您仍然可以为模块使用TypeScript,但您必须自己调用
define
而不是使用顶级export
关键字。感谢您的回答,我编辑了我的帖子,在那里创建了一个模块,并添加了define,然后取出了导出和顶级模块。这就是你说的吗?
var TestClass = (function () {
function TestClass(testValue) {
this.testValue = testValue;
}
TestClass.prototype.getTestValue = function () {
return this.testValue;
};
return TestClass;
})();
if (typeof window['define'] === "function" && window['define']['amd']) {
window['define']("TestClass", [], function () {
return TestClass;
});
}