简体   繁体   English

在节点服务器中为Angular项目导入打字稿文件

[英]Importing typescript files for Angular project in node server

I am unable to import TS files that exist in my Angular project directories inside my Node server. 我无法导入节点服务器内Angular项目目录中存在的TS文件。

I've looked into various settings for a tsconfig.json file for the Node server specifically but have had no luck. 我专门研究了Node服务器的tsconfig.json文件的各种设置,但没有运气。

I run my node server via npm start like so "start": "nodemon --exec ts-node -- ./start.ts" 我通过npm start运行节点服务器,例如"start": "nodemon --exec ts-node -- ./start.ts"

My project structure looks something like this... 我的项目结构看起来像这样...

....
node_modules/
src/
  app/
    shared/
      models/
        entity.model.ts
stub-server/
  start.ts
  src/
    data/
      entity-data.ts
angular.json
tsconfig.json
...

I expect to be able to import the relevent Typescript classes/interfaces/enums etc from entity.model.ts inside entity-data.ts so that I can enforce type safety within my mocked data. 我希望能够从entity-data.tsentity-data.ts导入相关的事件脚本类/接口/枚举等, entity.model.tsentity-data.ts数据中实施类型安全。

You definitely need to use better tooling for your project. 您肯定需要为您的项目使用更好的工具。
Please check https://nx.dev/ and create your Angular app and Node app, move your code there, and then you will be able to create a library with your shared stuff, to be imported from both projects. 请检查https://nx.dev/并创建Angular应用程序和Node应用程序,将代码移动到此处,然后您将能够创建包含共享内容的库,以从两个项目中导入该库。

Nx will take care of the configuration and the tsconfig paths required to make it work. Nx将负责配置和使其工作所需的tsconfig路径。 And talking about tsconfig.paths , you may try to setup one in your current project, perhaps this structure is not clean: 谈论tsconfig.paths ,您可以尝试在当前项目中设置一个,也许这种结构不干净:

"compilerOptions": {
  "paths": {
    "@shared/*": ["src/app/shared/*"]
  }
}

and import your stuff from your server like 并从服务器导入您的东西,例如

import { MyModel } from '@shared/models';

Anyways, consider the migration of your code to Nx and you will be able to enjoy a clean architecture and workflow. 无论如何,考虑将代码迁移到Nx,您将能够享受干净的体系结构和工作流程。 Happy coding! 编码愉快!

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

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