本文介绍了TypeScript替换<参考路径/>进口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一些TypeScript应用程序并得出结论我不喜欢< reference path /> 语句。我发现更适合使用 import'something'

I'm trying to write some TypeScript application and came up to conclusion that i don't like <reference path /> statement. I find it more suitable to use import 'something' instead.

但是当我尝试替换我的引用路径时我一直收到模块未知的错误。

But when I try to replace my reference paths i keep getting an error that module is unknown.

这是我的文件夹结构:

/app
    /ViewModels
        ApplicationViewModel.ts
    app.ts

ApplicationViewModel.ts

ApplicationViewModel.ts

module ApplicationVM {
    export class ApplicationViewModel {
        constructor(public test:string) {
        }
    }
}

app .ts

/// <reference path="ViewModels/ApplicationViewModel.ts" />

var a = new ApplicationVM.ApplicationViewModel('test');

这个工作得很好。我如何获得以下代码?

this one works just fine. How do i get following code to work as well?

import * as App from 'noidea';
var a = new App.ApplicationViewModel('test');

对于'noidea'我试过:'ViewModels / ApplicationViewModel','ApplicationVM'asl。我甚至将它与< reference path /> 组合在一起,但它也没有帮助。

For 'noidea' i tryed: 'ViewModels/ApplicationViewModel', 'ApplicationVM' asl. I even combined it with <reference path /> but it didn't help as well.

推荐答案

从ApplicationViewModel.ts中删除模块声明(如果使用外部模块,每个文件已经是一个模块):

Remove module declaration from ApplicationViewModel.ts (each file is already a module if you use external modules):

export class ApplicationViewModel {
    constructor(public test:string) {
    }
}

然后在你的app.ts

Then in your app.ts

import * as App from './ViewModels/ApplicationViewModel';
var a = new App.ApplicationViewModel('test');

这篇关于TypeScript替换&lt;参考路径/&gt;进口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 01:29