Javascript/Typescript - Olika syntax för att importera klass
Vad är skillnaden mellan två syntaxer för att importera i Angular:
import partition from 'lodash/fp/partition';
import { partition } from 'lodash/fp/partition';
Den första fungerar men den andra fungerar inte för mig.
Tack
Svar #1
Den första används för att importera något som är markerat som 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
Den andra låter dig specificera vad som ska importeras:
// lodash/fp/partiotion
export const a = 5;
export const b = 7;
export const partition = () => {}; // you are directly importing only this line
Svar #2
Den första importerar standardexporten från modulen 'lodash/fp/partition' medan den andra försöker destrukturera modulens exports och endast få partition-exporten.
För den här modulen:
module.exports = {
default: 1,
partition: 2,
}
import partition från 'lodash/fp/partition'; skulle vara:
partion === 1
import { partition } från 'lodash/fp/partition'; skulle vara:
partion === 2
För denna modul:
module.exports = 1
import partition från 'lodash/fp/partition'; skulle vara:
partion === 1
import { partition } från 'lodash/fp/partition'; skulle vara:
partion === undefined
