lakhan_Ideavate
lakhan_Ideavate

Reputation: 374

Javascript/Typescript - Different syntax to import class

What is different between two syntax to import in angular:

import partition from 'lodash/fp/partition';
import { partition } from 'lodash/fp/partition';

First one is working but other is not working for me.

Thanks

Upvotes: 2

Views: 137

Answers (2)

Yevhenii Dovhaniuk
Yevhenii Dovhaniuk

Reputation: 1103

The first one is used to import something that is marked as default:

// lodash/fp/partiotion

export const a = 5;
export const b = 'b';
export default {key: 'value'}; // only this line will be imported using the first syntax

The second one allows you to specify what to import:

// lodash/fp/partiotion

export const a = 5;
export const b = 7;
export const partition = () => {}; // you are directly importing only this line

Upvotes: 7

Teddy Sterne
Teddy Sterne

Reputation: 14201

The first one imports the default export from module 'lodash/fp/partition' whereas the second one attempts to destructure the module's exports and get just the partition export.

For this module:

module.exports = {
  default: 1,
  partition: 2,
}

import partition from 'lodash/fp/partition'; would be:

partion === 1

import { partition } from 'lodash/fp/partition'; would be:

partion === 2

For this module:

module.exports = 1

import partition from 'lodash/fp/partition'; would be:

partion === 1

import { partition } from 'lodash/fp/partition'; would be:

partion === undefined

Upvotes: 2

Related Questions