[英]Loading Mustache as AMD module with RequireJS - Mustache is not defined
我無法將mustache.js作為帶有RequireJS的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);
};
}
雖然在TestModel.js之前加載了mustache.js,但我收到一個javascript錯誤,說“Mustache未定義”。 我只是從這里使用mustache.js: https : //github.com/janl/mustache.js所以我想我可能不明白AMD模塊是如何正確聲明的。
我看到有一些“AMD包裝”作為這個項目的一部分,但我似乎無法弄清楚它們是如何被使用的。
Mustache確實將自己定義為AMD模塊 ,因此不需要對其進行填充。 我不知道TypeScript以及它如何與RequireJS集成,但嘗試從配置的shim數組中刪除Mustache。
我不太了解TypeScript,但文檔提到編譯器選項會將生成的javascript從CommonJS更改為AMD。 ( - 模塊--module amd
)
希望這可以幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.