Angular Basics: Importing Multiple Classes with the Same Name

A simple trick to Import two or more classes if they are sharing the same name.1 min


Import-Multiple-Exported-Class-With-Same-Name

Learn how to import multiple exported classes with the same name in Angular. A step-by-step guide for developers.

Requirement:

Let’s say I have two classes which share the same name among both the classes as shown below in the example.

import {CommonClassName} from '../module1/CommonClassName';
import {CommonClassName} from '../module2/CommonClassName';

In this case, you can use as an alias as given below. You can change AliasName at your convenience.

import {CommonClassName} from '../module1/CommonClassName';
import {CommonClassName as AliasName} from '../module2/CommonClassName';...
//You can call particular class with AliasName, as given below.this.modelClass = new AliasName();

If you have lengthy class names for your project, you can short it using an alias as.

Bingo! 👏👏👏

You did it! You can give any name in an alias and use any class if they are sharing the same name!

Curious to know more about Angular/Javascript “Import statement”?

Import is static, and so The static import statement is used to import read-only live bindings which are exported by another module.

Imported modules are in strict mode whether you declare them as such or not. The import statement cannot be used in embedded scripts unless such script has a type="module". Bindings imported are called live bindings because they are updated by the module that exported the binding.

There is also a function-like dynamic import(), which does not require scripts of type="module" Backward compatibility can be ensured using the attribute nomodule on the <script> tag.

There are a number of varieties available for Import Syntax.

import defaultExport from "module-name";
import * as name from "module-name";
import { export1 } from "module-name";
import { export1 as alias1 } from "module-name";
import { export1 , export2 } from "module-name";
import { foo , bar } from "module-name/path/to/specific/un-exported/file";
import { export1 , export2 as alias2 , [...] } from "module-name";
import defaultExport, { export1 [ , [...] ] } from "module-name";
import defaultExport, * as name from "module-name";
import "module-name";
var promise = import("module-name");

Explanation

  • defaultExport
    Name that will refer to the default export from the module.
  • module-name
    The module to import from. This is often a relative or absolute path name to the .js file containing the module. Certain bundlers may permit or require the use of the extension; check your environment. Only single quoted and double-quoted Strings are allowed.
  • name
    Name of the module object that will be used as a kind of namespace when referring to the imports.
  • exportN
    Name of the exports to be imported.
  • aliasN
    Names that will refer to the named imports.

Simplifying syntax for you — So you can remember it easily.

1. Import a single export from a module

import {myExport} from '/modules/my-module.ts';

2. Import an entire module’s contents

import * as myModule from '/modules/my-module.d.ts';

3. Import multiple exports from module

import {foo, bar} from '/modules/my-module.ts';

4. Import an export with a more convenient alias

import {reallyReallyLongModuleExportName as shortName}
  from '/modules/my-module.js';

5. Rename multiple exports during import

import {
  reallyReallyLongModuleExportName as shortName,
  anotherLongModuleName as short
} from '/modules/my-module.js';

6. Import a module for its side effects only

import '/modules/my-module.js';

Hope you find your answer with a good explanation and with some more knowledge about import. Please comment if it works for you or you find this article helpful.


References: 

Import statement details — Mozilla Developers

Cover Image by Author | BeingCoders - Learn angular basics for free.

adsense


Discover more from 9Mood

Subscribe to get the latest posts sent to your email.


Like it? Share with your friends!

What's Your Reaction?

Lol Lol
0
Lol
WTF WTF
0
WTF
Cute Cute
0
Cute
Love Love
1
Love
Vomit Vomit
0
Vomit
Cry Cry
0
Cry
Wow Wow
1
Wow
Fail Fail
0
Fail
Angry Angry
0
Angry
Rakshit Shah

Legend

Hey Moodies, Kem chho ? - Majama? (Yeah, You guessed Right! I am from Gujarat, India) 25, Computer Engineer, Foodie, Gamer, Coder and may be a Traveller . > If I can’t, who else will? < You can reach out me by “Rakshitshah94” on 9MOodQuoraMediumGithubInstagramsnapchattwitter, Even you can also google it to see me. I am everywhere, But I am not God. Feel free to text me.

0 Comments

Leave a Reply

Choose A Format
Story
Formatted Text with Embeds and Visuals
List
The Classic Internet Listicles
Ranked List
Upvote or downvote to decide the best list item
Open List
Submit your own item and vote up for the best submission
Countdown
The Classic Internet Countdowns
Meme
Upload your own images to make custom memes
Poll
Voting to make decisions or determine opinions
Trivia quiz
Series of questions with right and wrong answers that intends to check knowledge
Personality quiz
Series of questions that intends to reveal something about the personality
is avocado good for breakfast? Sustainability Tips for Living Green Daily Photos Taken At Right Moment