[英]Combine multiple "import * as <name> from '<path>' which contains multiple export const into 1 export for index.js
假设我有 2 个文件,比如说filters.js
和utils.js
。 两者都包含多个export const
,如下所示:
实用程序.js
export const formatDate = (
value,
formatting = { month: 'short', day: 'numeric', year: 'numeric' }
) => {
if (!value) return value
return new Intl.DateTimeFormat('en-US', formatting).format(new Date(value))
}
/**
* Return short human friendly month representation of date
* Can also convert date to only time if date is of today (Better UX)
* @param {String} value date to format
* @param {Boolean} toTimeForCurrentDay Shall convert to time if day is today/current
*/
export const formatDateToMonthShort = (value, toTimeForCurrentDay = true) => {
const date = new Date(value)
let formatting = { month: 'short', day: 'numeric' }
if (toTimeForCurrentDay && isToday(date)) {
formatting = { hour: 'numeric', minute: 'numeric' }
}
return new Intl.DateTimeFormat('en-US', formatting).format(new Date(value))
}
// Strip all the tags from markup and return plain text
export const filterTags = (value) => value.replace(/<\/?[^>]+(>|$)/g, '')
过滤器.js
export const title = (value, replacer = ' ') => {
if (!value) return ''
const str = value.toString()
const arr = str.split(replacer)
const capitalizedArray = []
arr.forEach((word) => {
const capitalized = word.charAt(0).toUpperCase() + word.slice(1)
capitalizedArray.push(capitalized)
})
return capitalizedArray.join(' ')
}
export const avatarText = (value) => {
if (!value) return ''
const nameArray = value.split(' ')
return nameArray.map((word) => word.charAt(0).toUpperCase()).join('')
}
现在,在我的index.js
中,我想组合 2 并再次导出它。 这是我不工作的代码
import * as Filters from './filters'
import * as Utils from './utils'
export { Filters, Utils }
这编译得很好,我可以使用import {avatarText } from '@utils'
我只是为他们的路径@utils
。
导入 avatarText 不起作用并进行调试,它显示了这个结果
每个模块都包含我作为export const
放置的方法的数组。
我该怎么做才能解决这个问题?
您是否已经尝试过:
// index.js
export * from './filters'
export * from './utils'
请注意,在这种情况下,您不能有名称冲突。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.