我无法使用 RequireJS 将 mustache.js 作为 AMD 模块加载(用于在 Backbone 模型中使用)。我也开始使用 TypeScript,因此我没有完全遵循依赖和声明的流程!
从 app.ts 开始:
require.config({
paths: {'jquery': 'lib/jquery-1.8.3.min', 'underscore': 'lib/underscore.min', 'backbone': 'lib/backbone.min', 'mustache': 'lib/mustache', 'appmain': 'AppMain'},
shim: {mustache: { deps: ["jquery"] }, backbone: { deps: ["underscore", "jquery"] }, appmain: {deps: ["backbone", "mustache"] } } });
require(['appmain'], (main) => {
var appMain = new main.AppMain();
appMain.run();
});
appmain.ts:
import tm = module("models/TestModel");
export class AppMain {
public run() {
var testModel = new tm.TestModel()
}
}
测试模型.ts:
/// <reference path="../modules/backbone.d.ts"/>
export class TestModel extends Backbone.Model {
constructor(options?) {
super(options);
};
initialize() {
var stringTemplate = "{{something}}";
Mustache.compile(stringTemplate);
};
}
我收到一个 javascript 错误,提示“未定义 Mustache”,尽管 mustache.js 确实在 TestModel.js 之前加载。我只是从这里使用 mustache.js:https ://github.com/janl/mustache.js所以我想我可能不明白 AMD 模块是如何正确声明的。
我看到这个项目中有一些“AMD 包装器”,但我似乎也无法弄清楚它们是如何使用的。