最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ecmascript 6 - Why should not declare const variable in export default line in JavaScript ES6? - Stack Overflow

programmeradmin1浏览0评论

I have declared const variable like,

"student.js"
 export default const mark= 20;

I am Calling this constant in index.js file

"index.js"
import {mark} from './student';
console.log("Mark Value ::::" + mark);

am getting error????

I have declared const variable like,

"student.js"
 export default const mark= 20;

I am Calling this constant in index.js file

"index.js"
import {mark} from './student';
console.log("Mark Value ::::" + mark);

am getting error????

Share Improve this question asked Feb 15, 2018 at 7:25 SuryaSurya 231 silver badge5 bronze badges 4
  • 2 am getting error???? You are asking or telling? – Rajesh Commented Feb 15, 2018 at 7:26
  • import mark from './student'; – Surya Commented Feb 15, 2018 at 7:26
  • it wont work . am asking the reason. – Surya Commented Feb 15, 2018 at 7:27
  • 1 export default means you are exporting a module. export const means you are exporting part of a module and module will be formed later after bining all exports. So you should get error on export default const itself – Rajesh Commented Feb 15, 2018 at 7:29
Add a ment  | 

3 Answers 3

Reset to default 9

export default expects an expression. While const is a statement.

You can't do export default const mark = 20 for the same reason you can't do console.log(const mark = 20).

If mark isn't used anywhere else in this file, it should be:

export default 20;

Otherwise it should be:

const mark = 20;
export default mark;

And imported like:

import mark from './student';

Adding to @estus answer, for your code to work change as follows.

"student.js"

export const mark = 20;

"index.js"

import {mark} from './student';
console.log("Mark Value ::::" + mark);

What happens on export default var a = 10, b = 20, c = 30?

Check out this discussion.

发布评论

评论列表(0)

  1. 暂无评论