繁体   English   中英

如何将一个VS2013项目中的通用打字稿代码共享到一个单独的VS2013项目中

[英]How do I share common typescript code from one VS2013 project into a separate VS2013 project

我将Visual Studio 2013,Typescript 1.3和模块系统设置为AMD(通过Nuget下拉的requirejs 2.1.15,RequireJS 2.1.8的类型定义)

我想要一个Web应用程序,我们称它为MyWebApplication1,它有一个简单的html页面,其中包括一个从我的打字稿生成的javascript文件。 MyTypescript.ts

MyHtml.html:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script type="text/javascript" src="Scripts/require.js" data-main="Scripts/requireConfig.js">   </script>
</head>
<body>
    <div id="result">DEFAULT</div>
</body>
</html>

requireConfig.ts:

require.config({
    baseUrl: 'Scripts',  
});
require([ '../Scripts/MyTypescript'],
    function (MyTypescript) {
        MyTypescript.MyFunction("Mr Anderson");
    }
);

MyTypescript.ts:

import MyCommonResource = require("MyCommonResource");
export = MyApp;
module MyApp {
    export function MyFunction(myParameter: string) {
        var aString: string = (new Date()).toTimeString() ;
        var myCommonThing: MyCommonResource.CommonClass = new MyCommonResource.CommonClass();
        aString = myCommonThing.CommonMethod();
        alert("HELLO " + myParameter + " @  " + aString);
    }
}

MyCommonResource.ts:

export = MyCommon;
module MyCommon {
    export class CommonClass {
        constructor() { }
        public CommonMethod(): string {
            return "COMMON";
        }
    }
}

在一个项目中,以下文件夹结构都可以很好地工作:

MyWebApplication1
  -Scripts
    -MyTypescript.ts
    -MyCommonResource.ts
    -requireConfig.ts
  -MyHtml.html

但是,我想要这样的东西(各种Web项目可能需要一个单独的项目)

MyWebApplication1
  -Scripts
    -MyTypescript.ts
    -requireConfig.ts
  -MyHtml.html
MyCommonWebCode1
  -Scripts
    -MyCommonResource.ts

在MyTypescript.ts中没有这样的东西,我看不到实现此目的的好方法:

import MyCommonResource = require("..\..\AnotherProject\Scripts\MyCommonResource");

这对于在IIS下进行部署不起作用,因为生成的js定义如下所示:

define(["require", "exports", "..\..\AnotherProject\Scripts\MyCommonResource"], function (require, exports, MyCommonResource) {

有没有针对这种情况的好的解决方案?

我考虑过使用构建后事件,使用本地nuget服务器,...使用打字稿和Visual Studio时,似乎并不是处理不同Web应用程序中通用代码段的好方法。

提前致谢。

您已经提到了我认为正确的选择。

如果您有要在多个项目中共享的代码,则包管理器是共享代码的理想方法。 它不仅有助于共享代码的分发,还有助于版本控制。

有一些巧妙的扩展使NuGet软件包的创建非常容易-我正在使用NuGet Packager

您可以使用Pro Get的免费版本开始使用-这是一台非常整洁的NuGet服务器。

您还可以将本地NuGet服务器用作代理,以保持其他库版本的一致性,保留“团队批准的”软件包的列表,并确保已检查许可证并遵守许可证条件(而不是必须检查是否有人添加了一个新包)。

如果您希望使用其他软件包管理器(即npm),则可以遵循相同的模式,因为功能非常相似。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM