导入位于比当前路径低的路径中的TypeScript模块会引发范围错误 [英] Importing TypeScript Module located in path lower than current path throws Scope Error

查看:88
本文介绍了导入位于比当前路径低的路径中的TypeScript模块会引发范围错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在试图将AMD友好的TypeScript应用程序框架组合在一起时,我遇到了一个麻烦:我似乎无法从当前路径中退出,以将模块导入另一个目录.我可以导入上面的模块,但是下面的模块会引发错误:

In an attempt to put together an AMD-friendly TypeScript application skeleton, I've run into a snag: I can't seem to drop down from my current path to import a module in another directory. I can import modules that are above, but below throws an error:

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

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

Here the is the structure of my (very basic) app:

app/
 - core/
    - View.ts
 - views/
    - HomeView.ts
 - Application.ts

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

In my Application.ts file, I can successfully import a module like so:

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

export class Application {
    constructor() {
        console.log( 'initializing Application' );
    }
}

在使用--module AMD标志时,正确输出

Which, when using the --module AMD flag, correctly outputs

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

现在,问题是当我跳入views/HomeView.js并尝试导入我的core/View BaseClass来从以下内容扩展时:

Now, the problem is when I jump into views/HomeView.js and attempt to import my core/View BaseClass to extend from:

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

export class HomeView {
    constructor() {
        console.log('Hello HomeView!');
    }
}

哪个抛出完整错误:

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');
---------------------------^^^^^^^^^^^^^^--

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

Is this a bug in the compiler, or is my understanding of module imports faulty? Why would i be able to import views/HomeView, but not ../core/View?

任何帮助将不胜感激.

Any help would be greatly appreciated.

推荐答案

尽管我无法告诉您为什么您的相对路径不起作用,我还是设法使用了根路径来实现它.

I managed to get this to work using a root path - although I can't tell you why your relative path doesn't work.

import view = module("./app/core/View");

这篇关于导入位于比当前路径低的路径中的TypeScript模块会引发范围错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆