两个项目之间的跨项目引用 [英] Cross-project references between two projects

查看:435
本文介绍了两个项目之间的跨项目引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在两个TypeScript项目之间进行引用?假设我们具有以下项目结构:

Is it possible to make a reference between two TypeScript projects? Assume we have the following project structure:

Module1.ts包含:

module TestModule {
    export interface Interface1 {
    }
}

Module2.ts包含:

module TestModule {
    export interface Interface2 extends Interface1 {
    }
}

Test1Test2中被引用.我在Module2.ts中收到错误Could not find symbol 'Interface1'.它可以在一个项目中运行,但是我不知道如何使它在另一个项目中可见...也许现在还不可能.

Test1 is referenced in Test2. I get an error Could not find symbol 'Interface1' in Module2.ts. It works within one project, but I don't know how to make it visible from the other project... Maybe it's not possible for now.


当我尝试使用TestModule.Interface1模式时,出现相同的错误(用不同的方式表示).但是IntelliSense看到了我的Interface1:


When I try to use TestModule.Interface1 pattern, I get the same error (said in different way). But the IntelliSense sees my Interface1:

.
我注意到我不能使用其他项目中的文件.即使我添加了正确的引用(/// <reference ...)并链接了我的第一个项目中的所有文件.


I have noticed I can't use files from the other project. Even if I have a correct reference (/// <reference ...) added and linked all the files in my 1st project.

推荐答案

有很多方法可以做到这一点.

There's lots of ways you can do this.

选项1-项目参考(TypeScript 3.0 +)

如果您使用的是TypeScript 3.0,请查看使用项目引用.在此处了解更多>.

If you are using TypeScript 3.0 then look at using project references. Read more here.

选项2-构建脚本

使用类似 gulp-typescript

Use something like gulp-typescript, grunt-ts, or just a batch script to copy the files over into a folder in the main project.

或者,运行构建事件在Visual Studio中,它将文件复制到主项目中.

Alternatively, run a build event in Visual Studio that will copy the files over to the main project.

选项3-npm程序包

如果使用npm,则可以为其他项目创建一个包.然后,您可以在主项目中使用您的包. 指定本地依赖项是执行此操作或使用类似

If you use npm, you could create a package for your other project. Then you can use your package in your main project. Specifying a local dependency is a good way to do this or by using something like sinopia, which is a private repository server. I've never used it, but it looks like it would work well.

选项4-NuGet软件包

您可以查看创建nuget包,然后在本地安装.

选项5---declaration --outDir编译器选项

Option 5 - --declaration --outDir compiler option

您可以在 与其他项目的目录,然后也使用--declaration对其进行编译,以便它也生成声明文件(.d.ts).例如:--declaration --outDir ../Test1/External.

You can set the --outDir compiler option or the outDir property in tsconfig.json with the directory to your other project then also compile it with --declaration so that it generates the declaration files (.d.ts) too. For example: --declaration --outDir ../Test1/External.

原始答案(使用--out)

Original Answer (Using --out)

如果右键单击库项目,然后单击属性,则可以在Visual Studio中执行类似的操作.在TypeScript Build选项卡中,取消选中Combine JavaScript output into file的选项,然后在主项目中指定要执行的位置(例如$(SolutionDir)/TypedApp/External/TypedLibrary.js).然后还要选中Generate declaration files以生成.d.ts文件.

You can do something similar in Visual Studio if you right click on your library project and click properties. In the TypeScript Build tab, check off the option to Combine JavaScript output into file and specify the location in your main project you want it to go (Ex. $(SolutionDir)/TypedApp/External/TypedLibrary.js). Then also check off Generate declaration files in order to generate a .d.ts file.

完成此操作后,构建您的库项目,然后在主项目中包含.js.d.ts.在HTML中包含.js文件,在打字稿文件中引用.d.ts.

Once this is done, build your library project and then include the .js, and .d.ts in your main project. Include the .js file in your html and reference the .d.ts in your typescript files.

每次重建库项目时,它将自动使用更改来更新主项目.

Each time you rebuild the library project, it will automatically update the main project with the changes.

这篇关于两个项目之间的跨项目引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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