Home > Software engineering >  what's the difference between exporting a component and importing its module in another module?
what's the difference between exporting a component and importing its module in another module?

Time:11-13

when working in Angular, let's say we have module A and module B, if I want to use "A-component" inside components of module B, then what's the difference between importing module A inside Module B , and adding the "A-component" in the exports of module A ?

I tried both, and I think they are not the same thing, this is confusing.

CodePudding user response:

You need to export and import, not just one.

Unless you use default exports, your code would need to look something like this:

a.mjs

export class myClass {
  ...
}

b.mjs

import { myClass as myClassFromA } from './a.mjs'

or with default exports:

a.mjs

export default class myClass {
  ...
}

b.mjs

import myClassFromA from './a.mjs'

CodePudding user response:

  1. importing module A inside Module B It means that you would like to access inside Module B components, pipes, directives and sevices from module A
  2. adding the "A-component" in the exports of module It means that you are making this component accessible for those who is using module A

CodePudding user response:

Angular based on Dependency injection. For example, you want to use A-component from B-module, you NEED to export A-component from A-module and import A-module in B-module.

What if you directly use A-component without exporting?

Then, you have to provide all dependencies of A-component in B-module, so it's a bad practice. It's better to export it from module.

  • Related