Can't js use import {} to get the properties of an object?

in a.js

export default {
  b: 1
}

then in b.js

import { b } from "./a"
console.log(b)

result error: "export "b"was not found in". / a"
babel/webpack environment
is it the wrong way I use it?

Mar.13,2021

export const b = 1;

then

import { b } from './a'

it's not the way you use it. It's something without export. Imort, module is not similar to object properties


export default is exported as a whole and can only be imported as a whole, not deconstructed.


export default {
  b: 1
}

then

import * as tmpname from './a';
console.log(tmpname.default.b)

or

import othername from './a';
console.log(othername.b)

teacher Ruan Yifeng's ES6 tutorial explains the knowledge in detail, including the usage of import, export :
Module syntax


import reference and deconstruction assignment cannot be taken as the same thing. Of course, this reference seems to be supported by babel plug-ins. But after all, it is not used correctly.

Menu