vuejs / composition-api

Composition API plugin for Vue 2
https://composition-api.vuejs.org/
MIT License
4.19k stars 343 forks source link

About expose #936

Closed LancerComet closed 2 years ago

LancerComet commented 2 years ago

This package didn't come with the function expose, but the way to implement it is quite straightforward:

const expose = (exposing: Record<string, any>) => {
  const instance = getCurrentInstance()
  if (!instance) {
    throw new Error('expose should be called in setup().')
  }

  const keys = Object.keys(exposing)

  keys.forEach(key => {
    instance.proxy[key] = exposing[key]
  })

  onBeforeUnmount(() => {
    keys.forEach(key => {
      instance.proxy[key] = undefined
    })
  })
}

const App = defineComponent({
  setup () {
    expose({
      greet: () => console.log('Greeting')
    })

    return () => (
      <div>Something</div>
    )
  }
})

Because using composition API doesn't need to take care of context and responsiveness, I think it is okay to assign functions to the instance directly, and it turns out it works quite well. So how about adding it back?

github-actions[bot] commented 2 years ago

Stale issue message

LancerComet commented 2 years ago

Since Vue 2.7 has its expose function, this issue has became unnecessary, closed.

cloydlau commented 1 year ago

It's still useful since @vue/composition-api is inherently for Vue 2.6.