简体   繁体   English

TypeScript:重用前端(Angular)和后端的接口和类

[英]TypeScript: Reuse of interfaces and classes for frontend (Angular) and backend

I have a monorepo for a current project based on a Frontend (Angular) and a Backend (developed with NestJS - so it's NodeJS). 我有一个基于前端(Angular)和后端(使用NestJS开发的当前项目的monorepo - 所以它是NodeJS)。 I want to use custom interfaces and classes for both - frontend and backend. 我想使用自定义接口和类 - 前端和后端。 For example, creating DTOs so my frontend knows the parameter of my backend. 例如,创建DTO,以便我的前端知道我后端的参数。

I thought about a common folder like the following project structure shows, but this is not working, because the common folder is outside the scope of Angular (tsconfig) and so auto completition isn't working 我想到了一个常见的文件夹,如下面的项目结构所示,但这不起作用,因为公共文件夹超出了Angular(tsconfig)的范围,因此自动完成不起作用

project
├── client (Angular)
├── server (NestJS)
└── common (client and server share specific interfaces and classes)

Does anyone has experience for this? 有没有人有这方面的经验? At the moment I add my interfaces to both folders, but this is is evil because if I update one interface, I have to replace the other, too. 目前我将接口添加到两个文件夹,但这是邪恶的,因为如果我更新一个接口,我也必须替换另一个接口。

In TypeScript 3.0 "Project references" were introduced. 在TypeScript 3.0中引入了“项目引用”。 This could help you achieve what you want (I was using it to share models between angular and cloud function) 这可以帮助你实现你想要的(我用它来分享角度和云功能之间的模型)

https://www.typescriptlang.org/docs/handbook/project-references.html https://www.typescriptlang.org/docs/handbook/project-references.html

What you need to do is add an external project reference to the tsconfig.json of the project that should reuse files from somewhere else 您需要做的是向项目的tsconfig.json添加一个外部项目引用,该引用应该重用来自其他地方的文件

{
  "compilerOptions": {
    // The usual config
  },
  "references": [
    { "path": "../my-other-project" }
  ]
}

A tool like Lerna helps with this sort of setup, from experience this is the easiest way to go about things without creating any private npm repos (which is another alternative). Lerna这样的工具有助于这种设置,从经验来看,这是最简单的方法,而不需要创建任何私有的npm repos(这是另一种选择)。

Essentially, you setup your angular and server packages to install the common package just like any other npm package and run lerna to create virtual links for each package. 基本上,您设置角度和服务器包以安装公共包,就像任何其他npm包一样,并运行lerna为每个包创建虚拟链接。 This way when you need them for intellisense the editor will go thru the virtual file links lerna creates during the bootstrap phase, thus allowing you to create any number of common packages without doing any heavy lifting to connect all the consumer packages. 这样,当您需要intellisense时,编辑器将通过lerna在引导阶段创建的虚拟文件链接,从而允许您创建任意数量的常见包,而无需进行任何繁重的连接以连接所有使用者包。

In your tsconfig.json add common to paths property: 在你的tsconfig.json中添加common to paths属性:

     "paths": {
       "@angular/*": ["node_modules/@angular/*"],
       "rxjs/*": ["node_modules/rxjs/*"],
       "common/*": ["../common/*"]
     }

Then: import myInterface from 'exported interface in common folder'; 然后: import myInterface from 'exported interface in common folder';

The best is to have an index.ts which exports all services, interfaces whatever: export * from './myFile' 最好的是有一个index.ts导出所有服务,接口无论如何: export * from './myFile'

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

相关问题 Angular 5 / Typescript:在名称空间中导出的类/接口 - Angular 5/Typescript: Classes/Interfaces exported within a namespace Angular 2前端,Golang后端 - Angular 2 frontend, Golang backend 如何在Angular 2中使用Typescript类和接口实现“模式”以进行表单验证? - How to implement a “schema” with Typescript classes and interfaces in Angular 2 for form validation? Angular 9 中的类和接口 - Classes and interfaces in Angular 9 Typescript 中接口和类的区别 - Difference between interfaces and classes in Typescript Spring 引导后端与 angular 前端 - Spring boot backend with angular frontend 用于后端和前端的 Angular 2 + Node JS - Angular 2 + Node JS for backend and frontend django后端和有角度的前端向后端发送请求 - django backend and angular frontend send request to backend 如果后端和前端是分别在Heroku中部署的,则从express应用程序访问angular(typescript)的req.session - Accessing req.session from express app to angular (typescript) if the backend and frontend was deployed separately in Heroku How to use Environment Variables for API Url (Angular 8, TypeScript, HTML and SCSS for FrontEnd and C# for BackEnd) - How to use Environment Variables for API Url (Angular 8, TypeScript, HTML and SCSS for FrontEnd and C# for BackEnd)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM