Javascript 类型脚本、RequireJS垫片、环境模块声明
我已经得到了以下内容,它指向了主干网d.ts的定义Javascript 类型脚本、RequireJS垫片、环境模块声明,javascript,requirejs,typescript,js-amd,Javascript,Requirejs,Typescript,Js Amd,我已经得到了以下内容,它指向了主干网d.ts的定义 import Backbone = module("../../../dep/backbone/backbone"); export class Codebook extends Backbone.Model { defaults() { return { id: -1, title: -1 } } initialize() {
import Backbone = module("../../../dep/backbone/backbone");
export class Codebook extends Backbone.Model {
defaults() {
return {
id: -1,
title: -1
}
}
initialize() {
// do nothing
}
}
通过--模块amd
生成以下内容
define(["require", "exports", "../../../dep/backbone/backbone"], function(require, exports, __Backbone__) {
我已经用我的RequireJS配置文件填充了主干,因为它是全局的。我想让我的定义只说
主干
,而不是相对路径。除了构建后的过程之外,还有什么解决方法来操作define吗?我认为解决这个问题的方法是将环境声明放在与最终实际模块相同的位置,以便您可以使用相同的路径引用它
因此,
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: '_'
}
}
});
通过始终使用来自根的路径,该路径与使用相对路径时保持相同。这是一个黑客,但它对我有效()。我刚刚在上建立了一个博客 首先,只需使用到主干的参考路径,如下所示:
/// <reference path="../../modules/Backbone.d.ts" />
然后是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();
});
一旦应用程序到达所需的代码块,主干将被全局定义。玩得开心,它在同一个位置,但是shim为我处理导出和依赖关系,而不必将真正的backbone.js作为一个模块。
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();
});