我目前正在使用一个客户端构建,在TypeScript中使用TypeScript 1.x,在C#中使用AspNet WebApi后端。
为了与后端通信,我从角度使用$resource。通过使用此服务,我用TypeLITE生成的模型接口必须扩展
ng.resource.IResource<IModel> 是否可以自动扩展由角描述的这个接口生成的所有模型接口?
否则,一切都要像这样分开做
export interface Model extends ng.resource.IResource<Model> {
}非常感谢,MacX
发布于 2017-02-01 20:41:18
实现这一点以扩展TsGenerator的最简单方法。在与您的TsAngularJsModelGenerator.ttinclude文件相同的目录中创建一个名为TypeLite.Net4.tt的文件,并粘贴以下内容:
<#@ assembly name="$(TargetDir)\TypeLite.dll" #>
<#@ assembly name="$(TargetDir)\TypeLite.Net4.dll"#>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="TypeLite" #>
<#@ import namespace="TypeLite.TsModels" #>
<#+
class TsAngularJsModelGenerator : TsGenerator
{
protected override void AppendClassDefinition(TsClass classModel, ScriptBuilder sb, TsGeneratorOutput generatorOutput)
{
string typeName = this.GetTypeName(classModel);
string visibility = this.GetTypeVisibility(classModel, typeName) ? "export " : "";
sb.AppendFormatIndented("{0}interface {1}", visibility, typeName);
if (classModel.BaseType != null)
{
sb.AppendFormat(" extends {0}", this.GetFullyQualifiedTypeName(classModel.BaseType));
}
else
{
sb.AppendFormat(" extends ng.resource.IResource<{0}>", typeName);
}
sb.AppendLine(" {");
var members = new List<TsProperty>();
if ((generatorOutput & TsGeneratorOutput.Properties) == TsGeneratorOutput.Properties)
members.AddRange(classModel.Properties);
if ((generatorOutput & TsGeneratorOutput.Fields) == TsGeneratorOutput.Fields)
members.AddRange(classModel.Fields);
using (sb.IncreaseIndentation())
{
foreach (var property in members)
{
if (property.IsIgnored)
continue;
var propTypeName = this.GetPropertyType(property);
sb.AppendLineIndented(string.Format("{0}: {1};", this.GetPropertyName(property), propTypeName));
}
}
sb.AppendLineIndented("}");
_generatedClasses.Add(classModel);
}
} #>注意:您可能需要更改前两行以指向有效的位置(我发现,在MVC下,bin目录并不总是包含TypeLite.dll和TypeLite.Net4.dll二进制文件--如果遇到困难,记得在TypeLite.Net4.tt文件的第一行中将debug的值设置为true以获得更好的诊断信息)。
接下来,在TypeLite.Net4.tt文件中添加<#@include file="Manager.ttinclude"#>行下面的一行
<#@include file="TsAngularJsModelGenerator.ttinclude"#>最后,在TypeLite.Net4.tt文件中更改对TypeScript.Definitions的调用,传递TsAngularJsModelGenerator的一个新实例,如下所示:
<# var ts = TypeScript.Definitions(new TsAngularJsModelGenerator())
.WithReference("Enums.ts")
.ForLoadedAssemblies();
#>https://stackoverflow.com/questions/32049119
复制相似问题