简体   繁体   中英

how to export object of functions to another object of function javascript (nuxt)?

in index.js . This is working as expected

export default ({ $axios, target }) => () => ({
  async index({ endpoint }) { // Function that I want to reuse
    return await $axios.get(endpoint)
  },
  async specificFunction({ endpoint }) { // Function that is specific to this file
    return await $axios.get(endpoint/specific)
  },
})

but I want extract the index() function to another file for reusability

in shared.js I have tried

export default ({ $axios, target }) => () => ({
  async index({ endpoint }) { // Function that I want to reuse
    return await $axios.get(endpoint)
  }
})

I have also tried

export default ({ $axios, target }) => ({
  async index({ endpoint }) { // Function that I want to reuse
    return await $axios.get(endpoint)
  }
})

in index.js

import sharedFunction from './shared.js'

    export default ({ $axios, target }) => () => ({
      ...sharedFunction,
      async specificFunction({ endpoint }) { // Function that is specific to this file
        return await $axios.get(endpoint/specific)
      },
    })

I did try different way when spreading the function

import sharedFunction from './shared.js'

    export default ({ $axios, target }) => () => ({
      ...sharedFunction($axios, target),
      // ...sharedFunction($axios, target)(),
      async specificFunction({ endpoint }) { // Function that is specific to this file
        return await $axios.get(endpoint/specific)
      },
    })

In shared.js:

export default ({ $axios, target }) => ({
  index: async ({ endpoint }) => await $axios.get(endpoint)
})

In index.js:

import shared from './shared.js'

export default ({ $axios, target }) => ({
  ...shared({ $axios, target }),
  async specificFunction({ endpoint }) { // Function that is specific to this file
    return await $axios.get(endpoint/specific)
  },
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM