简体   繁体   English

如何修复 NestJS 项目结构中导入的相对路径?

[英]How can I fix the relative path of imports inside NestJS project structure?

I'm following the article and I'm struggling with imports on the step with creating UserService :我正在关注这篇文章,并且在创建UserService的步骤中努力导入

import { Injectable } from '@nestjs/common';
import { InjectRepository } from "@nestjs/typeorm"; 
import { UserDto } from 'src/user/dto/user.dto'; 
import { Repository } from "typeorm"; 
import { UserEntity } from './../../entity/user.entity';

@Injectable()
export class UserService { 

constructor( 
@InjectRepository(UserEntity) private userRepository: Repository<UserEntity> 
}{}
 
create(user: UserDto): Promise<UserDto> {
   return this.userRepository.save(user); 
 }
 findAll(): Promise<UserDto[]> {
   return this.userRepository.find(); 
 } 
}

The problem is for lines:问题在于线路:

import { UserDto } from 'src/user/dto/user.dto';

and specifically:具体来说:

import { UserEntity } from './../../entity/user.entity';

在此处输入图像描述

Does anyone know what can be the cause of this problem?有谁知道这个问题的原因是什么? What can I fix from my side?我可以从我这边解决什么问题?

UserDTO:用户DTO:

import { IsNumber, IsString } from 'class-validator';
export class UserDto { 
@IsNumber() id: number;
@IsString() name: string;
@IsString() lastName: string; 
}

UserEntity :用户实体

import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
@Entity() 
export class UserEntity { 

@PrimaryGeneratedColumn() 
id: number;

@Column({ nullable: false }) 
name: string;

@Column({ name: 'last_name', nullable: false }) 
lastName: string; 
}

In the project of the given article, by the same principle :在给定文章的项目中,按照相同的原则

在此处输入图像描述

there are no any issues, but from my side I see errors related to the project imports.没有任何问题,但在我这边,我看到了与项目导入相关的错误。

Am I missing something?我错过了什么吗? Do I need to add the absoluth path instead of relative?我是否需要添加绝对路径而不是相对路径?

There's the full project structure .有完整的项目结构

I'm ready to provide more details if it helps to detect and fix the issue.如果有助于检测和解决问题,我准备提供更多详细信息。

Thank you in advance.先感谢您。

Dots inside folder names are generally not a good idea.文件夹名称中的点通常不是一个好主意。 And try并尝试

import { UserEntity } from './../../entity/user.entity/user.entity';

or even in a shorter way as:甚至以更短的方式:

import { UserEntity } from '../../entity/user.entity/user.entity';

and accordingly:因此:

import { UserDto } from '../../dto/user.dto/user.dto';

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

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