我在用RequireJS (在主干模型中使用)加载mustache.js作为AMD模块时遇到了问题。我也开始使用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()
}
}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);
};
}尽管在TestModel.js之前加载了mustache.js,但我得到了一个javascript错误,提示"Mustache is not defined“。我只是使用这里的mustache.js : AMD,所以我想我可能不理解https://github.com/janl/mustache.js模块是如何正确声明的。
我看到有一些"AMD包装器“作为这个项目的一部分,但我似乎也不知道它们是如何使用的。
发布于 2013-01-13 10:07:19
Mustache可以进行define itself as an AMD module,因此不需要对其进行垫片。我不知道TypeScript以及它是如何与RequireJS集成的,但请尝试从您的配置填充数组中删除Mustache。
发布于 2013-01-10 11:05:54
我不太了解TypeScript,但文档中提到编译器选项会将生成的javascript从CommonJS更改为AMD。(--module amd)
Visual Studio TypeScript Options
希望这能有所帮助
https://stackoverflow.com/questions/14245329
复制相似问题