简体   繁体   中英

NestJS: It is necessary to import twice for dependency injection?

I have this two files:

canton.service.ts

import { PrismaClient } from "@prisma/client";
@Injectable()
export class CantonService extends CrudService {
  constructor(protected readonly prisma: PrismaClient) {
    super(prisma, "canton", "provincia");
  }
}

crud.service.ts

import { PrismaClient } from "@prisma/client";
export class CrudService {
  constructor(
    protected readonly prisma: PrismaClient,
    protected readonly model: string,
    protected readonly fkey?: string
  ) { /* all my methods */ }
}

So... is necessary to import PrismaClient in both files? There's may be another approach?

Yes it's necessary. The import tells Typescript and JavaScript that the PrismaClient will be used in the file. Technically there are ways to just import once, but it's a pretty bad practice and not really followed anymore. Better to properly declare your file dependencies rather than relying on some global scope

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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