【问题标题】:Loading Mustache as AMD module with RequireJS - Mustache is not defined使用 RequireJS 将 Mustache 作为 AMD 模块加载 - 未定义 Mustache
【发布时间】:2013-01-13 10:07:19
【问题描述】:

我在使用 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()
    }
}

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 包装器”,但我似乎也不知道它们是如何使用的。

【问题讨论】:

  • 你的小胡子垫片需要导出一些东西
  • Mustache 也不依赖于 jQuery

标签: backbone.js requirejs typescript mustache


【解决方案1】:

Mustache 使用 define itself as an AMD module,因此不需要填充。我不知道 TypeScript 以及它如何与 RequireJS 集成,但请尝试从配置的 shim 数组中删除 Mustache。

【讨论】:

    【解决方案2】:

    我不太了解 TypeScript,但文档中提到编译器选项会将生成的 javascript 从 CommonJS 更改为 AMD。 (--module amd)

    Visual Studio TypeScript Options

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-07
      • 1970-01-01
      • 2021-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多