在打字稿中导入时出错

时间:2016-12-30 11:35:44

标签: javascript typescript typescript2.0

我创建了一个示例typescript jquery项目,我在导入时面临问题

错误是:

  

require.js:143未捕获错误:模块名称“ajaxLib”尚未出现   已加载上下文:_。使用require([])

home.ts:

/// <reference path="jquery.d.ts" />

import { AjaxContainer } from "./ajaxLib";
module Home {
    export class HomeContainer {
        private content: HTMLElement;

        constructor(value?: any) {
            // document.body.innerHTML = value;
            var animal = new AjaxContainer();
            var el: HTMLElement = document.getElementById('grid1');
            $(el).css({"color": "blue" });
        }
    }
}

window.onload = () => {
    var object1 = new Home.HomeContainer("ram");
}

完成项目在github:https://github.com/focode/typescriptJquery/tree/master

我也能成功使用jquery。

将模块更改为amd后,转换后的js文件现在包含define

define(["require", "exports", "./ajaxLib"], function (require, exports, ajaxLib_1) {
    "use strict";
    var Home;
    (function (Home) {
        var HomeContainer = (function () {
            function HomeContainer(value) {
                var animal = new ajaxLib_1.AjaxContainer();
                var el = document.getElementById('grid1');
                $(el).css({ "color": "blue" });
            }
            return HomeContainer;
        }());
        Home.HomeContainer = HomeContainer;
    })(Home || (Home = {}));
    window.onload = function () {
        var object1 = new Home.HomeContainer("ram");
    };
});

但在浏览器控制台上我得到了:

require.js:143 Uncaught Error: Mismatched anonymous define() module: function (require, exports, ajaxLib_1) {
    "use strict";
    var Home;
    (function (Home) {
        var HomeContainer = (function () {
            function HomeContainer(value) {
                var animal = new ajaxLib_1.AjaxContainer();
                var el = document.getElementById('grid1');
                $(el).css({ "color": "blue" });
            }
            return HomeContainer;
        }());
        Home.HomeContainer = HomeContainer;
    })(Home || (Home = {}));
    window.onload = function () {
        var object1 = new Home.HomeContainer("ram");
    };
}
http://requirejs.org/docs/errors.html#mismatch

0 个答案:

没有答案