TypeScript替换& lt;参考路径/>带进口 [英] TypeScript replace <reference path/> with import

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

问题描述

我正在尝试编写一些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.我什至将其与<参考路径/> 组合在一起,但效果也不佳.

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替换&amp; lt;参考路径/&gt;带进口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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