【问题标题】:TypeScript dynamic loading of AMD module ends in "Could not find symbol '...'AMD 模块的 TypeScript 动态加载以“找不到符号 '...' 结尾
【发布时间】:2014-02-22 23:57:53
【问题描述】:

不,这个话题不会回答我的问题,不,解决方案不仅仅是在 nav.ts 文件中导入Command。 nav.ts 是许多 viewModel 文件之一,它们将根据需要动态加载。唯一的问题是在类的构造函数中设置参数的类型。 (类型必须是“命令”)


在下面将由 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 中要求)

ma​​in.ts(入口点)

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));
    });
});

/编辑

问题:

Visual Studio 将抛出 error TS2095: Could not find symbol 'Command',因为“Command”类未在此模块中定义。

如果“Command”-Type 将从NavViewModel 构造函数中删除,则程序可以正常工作。是否有任何解决方案可以在 NavViewModel 中引用 Command 类?

这行不通:

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

【问题讨论】:

  • 您应该展示如何定义模块的代码。 Command 类与您的 NavViewModel 类在不同的模块中吗?如果是这样,您需要引用整个名称(例如command: Your.Module.Name.Command)。
  • 我添加了 sn-p,它显示了入口点。
  • @Louis 谢谢,但我不认为这是一个重复的问题,因为链接的示例不会在运行时动态加载另一个模块..
  • 好的,您需要将NavViewMain 的构造函数的参数设为接口(即ICommand),而不是具体的类,并让您的Command 类实现该接口。每个 viewModel 类都需要引用接口,这样你就可以放弃导入的需要

标签: visual-studio compilation requirejs typescript amd


【解决方案1】:

使用 RequireJS 时,import 语句应该是从应用程序的 root 开始的完整路径。

我还使用了稍微不同的导出语法

command.ts

class command {
    ...
}

export = command;

ma​​in.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,因为大型内部应用程序会受到版本之间一些重大更改的影响

【讨论】:

  • 感谢您的回复,但正如我所写(大约 4 次),脚本将在运行时动态加载,因此无法导入。在编译时,不清楚将加载哪些视图模型。该脚本运行良好,我只想知道是否可以告诉 VS NavViewModel 的命令参数类型为“Command”,而无需在每个视图模型中导入 Command 对象。
  • 那我完全误解了你的问题,因为它是在我回复时发布的。进一步的编辑改变了问题的性质。错误是在 nav.ts 中抛出的,而不是 main.ts 中?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-29
  • 2018-11-05
  • 2011-11-18
  • 1970-01-01
  • 2016-07-29
  • 2012-11-11
  • 2013-04-30
相关资源
最近更新 更多