Skip to content

Futurum Technology

Blog

Menu
  • HOME
  • OFFER
  • PROEJCTS
  • CUSTOMER VOICE
  • PARTNERSHIP
  • TEAM
  • BLOG
  • CONTACT
Menu
Futurum Technology | Import & export in ES6/Angular

Import & export in ES6/Angular

Posted on November 29, 2018

Introduction

When seeing for the first time Angular application, you may spot at the top of the components ‘import’ statements. This is due to the fact that Angular heavily relies on JavaScript ES6 and the ‘imports’ are part of it.

But what exactly ‘import’ means? To answer that question first we need to ask another one: what is ‘export’? ‘Export’ in JavaScript ES6 is way of saying ‘make this code available for other modules’. And with that knowledge we can answer the previous question, ‘import’ is the opposite of ‘export’ and it gets code from other modules. Another important thing to precise is what exactly ‘module’ is? Module is simply a piece of JavaScript code. Developers usually split code into different modules to keep code easy to maintain.

turned on gray laptop computer

Coding and examples

With knowledge from introduction we’re ready to study some code. Let’s say we’ve got file called ‘functions.ts’ with (as file name said) three different functions.

function hello() {   console.log('Hello'); }  function goodbye() {   console.log('Goodbye'); }  function greeting() {   console.log('Greetings'); }

These are simple functions which basically just log in the browser console appropriate welcome word.

To use the first function in a component we need to add an ‘export’ word before the function statement.

export function hello() {   console.log('Hello'); }

And then we need to import that function in component.

import { hello } from '../functions/functions';

Now we can fire that function for example on the button click. So let’s add it in the component view.

<button (click)="sayHello()">Click to say hello!</button>

And in component logic.

sayHello() {   hello(); }

Finally in browser console ‘Hello’ text should appear. Well done! This method is called ‘named export’. When importing a ‘named export’, you need to import an ‘export’ by name in curly braces.

But what if you want to import another function from the ‘functions.ts’ file?

export function goodbye() {   console.log('Goodbye'); }

Then in a component just add the name of this function into curly bracers.

import { hello, goodbye } from '../functions/functions';

And you’re good to go.

sayGoodbye() {   goodbye(); }

Sometimes you may need to import all functions from a file. That won’t be a problem, in that case you should use.

import * as functions from '../functions/functions';  sayhello() {   functions.hello(); }  sayGoodbye() {   functions.goodbye(); }

* (asterisk) means all, then you need to assign it to the some alias, in that case it’s ‘functions’.

There’s one more export method – ‘default export’.

export default function greeting() {   console.log('Greetings'); }

When you don’t specify certain name (omit curly braces) of ‘import’, the ‘default export’ will be imported.

import greeting from '../functions/functions';  greetUser() {   greeting(); }

In the ‘default export’ you can use random alias for ‘import’.

import farewell from '../functions/functions';  greetUser() {   farewell(); }

This will fire the same function as previous example and log into browser console ‘Greetings’. Bear in mind that in one file there could be only one ‘default export’.

Conclusion

As you can see, ‘import’/’export’ is fairly straightforward stuff.  You just need to remember that there are different ‘export’ methods – ‘named’ and ‘default’. And keeping that in mind, ‘import’ statement will also differ. All depends what you want to be imported into a component.

Our mission is to support startups in achieving success. Feel free to reach out with any inquiries, and visit our blog for additional tips. Tune in to our podcast to glean insights from successful startup CEOs navigating their ventures.

Explore:

About Author

Futurum Technology | Import & export in ES6/Angular

Futurum Technology Team

View all posts

Recent Posts

  • Navigating regulatory challenges for tech startups
  • Harnessing the power of open-source solutions for startups
  • The impact of emerging technologies on startup ecosystems
  • From MVP to market leader. Key milestones for startups
  • Creating a customer-centric startup from day one

Archives

  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • November 2018
  • September 2018
  • June 2018
  • April 2018
  • February 2018
  • January 2018
  • November 2017
  • October 2017

Categories

  • AI
  • Angular
  • application
  • Backend
  • Cybersecurity
  • danish
  • deutsch
  • dutch
  • english
  • estonian
  • finnish
  • frameworks
  • french
  • Front End
  • interviews
  • IT Outsourcing
  • italian
  • Java
  • JavaScript
  • marketing
  • Node.js
  • norwegian
  • our story
  • polish
  • product
  • React
  • recruitment
  • spanish
  • start-ups
  • swedish
  • Team Management
  • technology
  • Uncategorized
Futurum Technology
  • HOME
  • OFFER
  • PROJECTS
  • CUSTOMER VOICE
  • PARTNERSHIP
  • TEAM
  • BLOG
  • CONTACT
  • HOME
  • OFFER
  • PROJECTS
  • CUSTOMER VOICE
  • PRATNRTSHIP
  • TEAM
  • BLOG
  • CONTACT
🇩🇰 🇩🇪 🇪🇪 🇪🇸 🇫🇮 🇫🇷 🇬🇧 🇮🇹 🇳🇱 🇳🇴 🇵🇱 🇸🇪
Futurum Technology | Import & export in ES6/Angular

You need IT help?
We are happy to share our experience!
Click here!

Futurum technology - podcasts

Are you looking for startup tips?
Here are more of them!

©2025 Futurum Technology