2014-01-30 19 views
1

不,这个主题不会回答我的问题,不,这个解决方案不是简单地在nav.ts文件中导入Command。 TS是许多视图模型,文件中的一个,他们将根据需要动态加载。唯一的问题是,设置参数的类型在类的构造函数。(类型必须是“命令”)AMD模块的TypeScript动态加载结束于“无法找到符号”...'


在下面的类将被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(); 
    } 
} 

编辑: 这里是入口点

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

/EDIT

(在评论2的要求)问题:

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

如果将从NavViewModel构造函数中删除“命令”类型,程序将正常工作。有没有解决方案可以引用NavViewModel中的Command类?

这是行不通的:

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

可能重复的[如何导入其他TypeScript文件?](http://stackoverflow.com/questions/12930049/how-do-i-import-other-typescript-files) – Louis

+0

您应该显示如何定义模块的代码。 Command类是否与NavViewModel类不同?如果是这样,你需要引用全名(例如'command:Your.Module.Name.Command')。 –

+0

我已经添加了片段,它显示了入口点。 – mat

回答

1

当使用RequireJS,import语句应该是从应用程序的根完整路径。

我也用一个稍微不同的出口语法

command.ts

class command { 
    ... 
} 

export = command; 

为主。TS

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

var cmd = new Command(); 

注意

我使用打字稿0.9.1.1。我无法将我的机器升级到0.9.5,因为大型内部应用程序受版本之间的某些中断更改的影响

+0

感谢您的回复,但是由于我写了(约4次),脚本将在运行时动态加载,因此导入不是选项。在编译时,不清楚哪些视图模型会被加载。该脚本工作正常,我只想知道,如果我可以告诉VS,NavViewModel的命令参数的类型是“命令”,而无需在每个viewModel中导入Command对象。 – mat

+0

然后我完全误解了你的问题,因为它是在我回复时发布的。进一步的编辑已经改变了问题的性质。错误是在nav.ts中引发的,而不是main.ts? –

相关问题