[英]TypeScript, RequireJS shim, ambient module declaration
我有以下内容指向backbone.d.ts定义。
import Backbone = module("../../../dep/backbone/backbone");
export class Codebook extends Backbone.Model {
defaults() {
return {
id: -1,
title: -1
}
}
initialize() {
// do nothing
}
}
使用--module amd
会生成以下内容。
define(["require", "exports", "../../../dep/backbone/backbone"], function(require, exports, __Backbone__) {
由于全局,我使用RequireJS配置文件填充了主干。 我希望我的定义只是说backbone
而不是相对路径。 除了构建后的过程之外,还有任何解决方法来操作定义吗?
我认为这个问题的解决方案是将环境声明放在与最终实际模块相同的位置,这样您就可以使用相同的路径引用它。
所以backbone.d.ts
需要在相同的位置backbone.js
。
我总是使用根路径,而不是使用相对路径。 就像是:
import underscore = module('libs/underscore/underscore');
而黑客就是你可以在你的垫片中定义相同的名称。 像这样的东西:
require.config({
paths: {
'libs/underscore/underscore': 'libs/underscore/underscore'
},
shim: {
'libs/underscore/underscore': {
exports: '_'
}
}
});
通过始终使用来自根的路径,路径保持相同,就像使用相对路径一样。 这是一个黑客,但它适用于我( GitHub )。
我刚刚整理了一篇关于如何在TypeScript中使用AMD模块的博客。
首先,只需使用骨干的参考路径,如下所示:
/// <reference path="../../modules/Backbone.d.ts" />
然后定义你的类而不导入:
export class MTodoCollectionView extends Backbone.View {
...
}
然后你的require.config和apploader:
require.config({
baseUrl: '../',
paths: {
'underscore': 'lib/underscore',
'backbone': 'lib/backbone'
},
shim: {
underscore: {
exports: '_'
},
backbone: {
deps: ["underscore", "jquery"],
exports: "Backbone"
}
}
});
require(['jquery','underscore','backbone','console','app/AppMain',
],
($, _, Backbone, console, main) => {
// code from window.onload
var appMain = new main.AppMain();
appMain.run();
});
一旦应用程序遇到require代码块,Backbone就会被全局定义。
玩得开心,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.