AMD 模块的 TypeScript 动态加载以"找不到符号'..."结尾



否,这个主题不会回答我的问题,不,该解决方案不仅仅是在nav.ts文件中导入 Command。NAV.TS是许多ViewModel-Files之一,它们将按需动态加载。唯一的问题是在类的构造函数中设置参数类型。(类型必须为"命令")


在以下类中,将由require.js加载,方法viewModel()需要动态新类。在这种情况下,NavViewModel

command.ts

export class Command {

...

    public viewModel(name: string, callback: Function) {
        require(["noext!boot/getViewModel/" + name], function (viewModel) {
            callback(viewModel);
        });
    }
}

这是将由viewModel()获取的类:

nav.ts

export class NavViewModel extends kendo.Router {
    constructor(command: Command) {
        super();
        this.route('/:name', function (name) {
            command.view(name, $('div.content'));
        });
        this.start();
    }
}

编辑:这是入口点(注释2中请求)

main.ts (entrypoint)

import lib = require("command");
var cmd = new lib.Command();
cmd.viewModel('nav', function (o) {
    cmd.view('nav', $('div.header'), function () {
        kendo.bind($('.header .nav'), new o.NavViewModel(cmd));
    });
});

/edit

问题:

Visual Studio将投掷error TS2095: Could not find symbol 'Command',因为"命令"类未在此模块中定义。

如果"命令"型将从NavViewModel构造函数中删除,则程序正常。是否有任何解决方案可以在NavviewModel中引用命令类?

这不起作用:

/// <reference path="../../Scripts/command.ts" />

使用requirejs时,导入语句应为应用程序的 root 的完整路径。

我还使用略有不同的导出语法

command.ts

class command {
    ...
}
export = command;

main.ts

// I'm assuming the Scripts folder is at the root of the application
import Command = require('Scripts/command');
var cmd = new Command();

注意

我正在使用Typescript 0.9.1.1。我无法将机器升级到0.9.5,因为大型内部应用程序受版本之间的某些破坏变化的影响

最新更新