简体   繁体   English

AMD模块的TypeScript动态加载以“找不到符号'...”结束

[英]TypeScript dynamic loading of AMD module ends in "Could not find symbol '…'

No, this topic won't answer my question and NO, the solution is not simply importing Command in the nav.ts file. 不,这个主题不能回答我的问题,不,解决方案不只是在nav.ts文件中导入Command nav.ts is one of many viewModel-files and they will be loaded dynamically on demand. nav.ts是许多viewModel文件之一,它们将根据需要动态加载。 The only problem is to set the parameter's type in the constructor of the class. 唯一的问题是在类的构造函数中设置参数的类型。 (Type has to be "Command") (类型必须为“ Command”)


In the following class, which will be loaded by require.js, the method viewModel() requires a new class dynamically. 在以下类(将由require.js加载)中,方法viewModel()动态需要一个新类。 In this case NavViewModel . 在这种情况下NavViewModel

command.ts command.ts

export class Command {

... ...

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

This is the class which will be fetched by viewModel() : 这是将由viewModel()获取的类:

nav.ts nav.ts

export class NavViewModel extends kendo.Router {
    constructor(command: Command) {
        super();

        this.route('/:name', function (name) {
            command.view(name, $('div.content'));
        });

        this.start();
    }
}

EDIT: Here is the entry-point (requested in comment 2) 编辑:这是切入点(在评论2中要求)

main.ts (EntryPoint) 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 /编辑

The Problem: 问题:

Visual Studio will throw the error TS2095: Could not find symbol 'Command' , because the "Command" class ist not defined in this Module. Visual Studio将抛出error TS2095: Could not find symbol 'Command' ,因为在此模块中未定义“ Command”类。

The program works fine if the "Command"-Type will be removed from the NavViewModel constructor. 如果将从NavViewModel构造函数中删除“ Command” NavViewModel则程序可以正常NavViewModel Is there any solution to reference the Command class in the NavViewModel? 有什么解决方案可以在NavViewModel中引用Command类吗?

This won't work: 这行不通:

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

When using RequireJS, the import statement should be the full path from the root of the application. 使用RequireJS时,import语句应该是从应用程序根目录开始的完整路径。

I also use a slightly different export syntax 我还使用了稍微不同的导出语法

command.ts command.ts

class command {
    ...
}

export = command;

main.ts main.ts

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

var cmd = new Command();

Note 注意

I'm using Typescript 0.9.1.1. 我正在使用Typescript 0.9.1.1。 I can't upgrade my machine to 0.9.5 as a large internal application is affected by some breaking changes between versions 我无法将计算机升级到0.9.5,因为大型内部应用程序受到版本之间某些重大更改的影响

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM