javascript - 使用来自单个导出的导入*

标签 javascript

我不习惯经常看到导入*

我正在经历的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/

相关文章:

javascript - 删除 View 时取消所有传出的 REST HTTP 请求

javascript - 在异步循环中设置 useState Hook

javascript - 在 Three.JS 中处理大量独特的对象

javascript - PHP 文件未获取使用 POST 通过 AJAX 发送的变量

javascript - html 的 Jquery 错误

javascript - 无法让 Phaser 工作

javascript - Windows 2008 上的 JSON 语法错误

javascript - 在youtube iframe播放器中播放下一首和上一首歌

javascript - setTimeout(fun) 有一个参数? (未指定超时)

javascript - jquery 生成的按钮不起作用