【问题标题】:How can I import all export consts at the same time?如何同时导入所有导出常量?
【发布时间】:2020-09-03 14:07:10
【问题描述】:
export const getURLPath = (url) => url.split('.com')[1]
export const getSectionName = (path) => path.split('/butik/liste')[1]
export const getIdByName = (sectionName) => sectionMapping.find(item => item.name.toLowerCase() == sectionName).id
export const getNameById = (sectionId) => sectionMapping.find(item => item.id == sectionId).name.toLowerCase()
export const getContentIdFromURL = (path) => path.split('-p-').pop().split('?')[0];
export const getBoutiqueIdFromURL = (path) => path.split('boutiqueId=').pop().split('&')[0];
export const getMerchantIdFromURL = (path) => path.split('merchantId=').pop().split('&')[0];

我想导入右侧的所有功能。我想用他们的纯名。示例:getURLPath() 就像这样,不带前缀等。我该怎么做?

【问题讨论】:

  • 不要将代码发布为图像,复制粘贴和编辑是不可能的。改用代码块。
  • 对不起,我编辑了这个

标签: javascript node.js ecmascript-6


【解决方案1】:

 export default {
    getURLPath : (url) => url.split('.com')[1],
    getSectionName : (path) => path.split('/butik/liste')[1],
    getIdByName : (sectionName) => sectionMapping.find(item => item.name.toLowerCase() == sectionName).id,
    getNameById : (sectionId) => sectionMapping.find(item => item.id == sectionId).name.toLowerCase(),
    getContentIdFromURL : (path) => path.split('-p-').pop().split('?')[0],
    getBoutiqueIdFromURL : (path) => path.split('boutiqueId=').pop().split('&')[0],
    getMerchantIdFromURL : (path) => path.split('merchantId=').pop().split('&')[0],
}

【讨论】:

    【解决方案2】:

    如果您不想要 utils.getURLPath 语法,那么唯一的方法就是将它们全部拼出来,即

    import {getURLPath, getSectionName, getIdByName, getNameById, /* etc ... */} from '../utils';
    

    【讨论】:

      【解决方案3】:

      你可以这样使用:

      export const fun1...;
      export const fun2...;
      ...
      
      // This is not even mandatory I think but leaves u the choice to use prefix at some point. As u wish
      export default {
        fun1,
        fun2,
      }
      

      然后将它们导入到您的其他文件中,例如:

      import {
        fun1,
        fun2,
        ...
      } from '/your/file';
      
      // Then use them
      fun1();
      

      【讨论】:

        猜你喜欢
        • 2017-03-26
        • 2020-08-09
        • 2021-10-04
        • 1970-01-01
        • 2010-11-08
        • 2022-01-14
        • 2017-06-25
        • 2023-03-29
        相关资源
        最近更新 更多