我不习惯经常看到导入*
我正在经历的react + readux代码使用import *
根据firefox文档关于import *
This inserts myModule into the current scope, containing all the exports from the module in the file located in /modules/my-module.js.
假设我们的代码中有这样的导出语句(称为articleTypes.js)。
export const ARTICLES_FETCHED = 'articles.ARTICLES_FETCHED';
这样做,实际上可能会有意义我们没有使用上面的导出默认值
import * as types from './actionTypes'
但是如果我们像这样改变上面的行
export default const ARTICLES_FETCHED = 'articles.ARTICLES_FETCHED';
并做这样的事情
import types from './actionTypes'
它的工作方式与上面的代码相同吗?或者这真的会起作用吗?这是更好的方法吗?
最佳答案
But If we alter the above lines like this
export default const ARTICLES_FETCHED = 'articles.ARTICLES_FETCHED';
and do something like this
import types from './actionTypes'
Will it work the same way as the above code? Will it work the same way as the above code?
不,types
将等于'articles.ARTICLES_FETCHED'
另一方面,这个模块:
export default {ARTICLES_FETCHED: 'articles.ARTICLES_FETCHED'}
与此模块的行为相同:
export const ARTICLES_FETCHED = 'articles.ARTICLES_FETCHED'
使用 import * as myVar from 'module'
的主要好处是,您可以将 module
的所有导出内容封装在一个整洁的变量 myVar< 中
.
关于javascript - 使用来自单个导出的导入*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51213113/