簡體   English   中英

組合多個“import * as<name> 從 '<path> ' 其中包含多個 export const 到 index.js 的 1 個導出中</path></name>

[英]Combine multiple "import * as <name> from '<path>' which contains multiple export const into 1 export for index.js

假設我有 2 個文件,比如說filters.jsutils.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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM