导入位于低于当前路径的路径中的TypeScript模块会引发Scope Error

cnp*_*cnp 5 scope module amd typescript

为了整合一个AMD友好的TypeScript应用程序框架,我遇到了一个障碍:我似乎无法从我当前的路径下拉导入另一个目录中的模块.我可以导入上面的模块,但是下面会抛出一个错误:

TypeScript Error: The name ''../core/View'' does not exist in the current scope

这是我(非常基本的)应用程序的结构:

app/
 - core/
    - View.ts
 - views/
    - HomeView.ts
 - Application.ts
Run Code Online (Sandbox Code Playgroud)

在我的Application.ts文件中,我可以成功导入这样的模块:

import HomeView = module( 'views/HomeView' );

export class Application {
    constructor() {
        console.log( 'initializing Application' );
    }
}
Run Code Online (Sandbox Code Playgroud)

其中,当使用--module AMD标志时,正确输出

define(["require", "exports", 'views/HomeView'], function(require, exports, __HomeView__) {
    var HomeView = __HomeView__;

    var Application = (function () {
        function Application() {
            console.log('initializing Application', HomeView);
        }
        return Application;
    })();
    exports.Application = Application;    
})
Run Code Online (Sandbox Code Playgroud)

现在,问题是当我跳进views/HomeView.js并尝试导入我的core/ViewBaseClass以从:

import View = module('../core/View');

export class HomeView {
    constructor() {
        console.log('Hello HomeView!');
    }
}
Run Code Online (Sandbox Code Playgroud)

这引发了这个完整的错误:

TypeScript Error: The name ''../core/View'' does not exist in the current scope
File: test/src/app/views/HomeView.ts
Start: 21, Length: 14

Line: import View = module('../core/View');
---------------------------^^^^^^^^^^^^^^--
Run Code Online (Sandbox Code Playgroud)

这是编译器中的错误,还是我对模块导入的理解有误?为什么我能导入views/HomeView,但不是../core/View

任何帮助将不胜感激.

Fen*_*ton 5

我设法使用根路径让它工作 - 虽然我无法告诉你为什么你的相对路径不起作用.

import view = module("./app/core/View");
Run Code Online (Sandbox Code Playgroud)

  • 我使用了几乎相同的修复程序.路径总是相对于根类(即加载第一个模块的类),因此View.ts总是可以通过`module("core/View")来访问. (6认同)