vuejs / composition-api

Composition API plugin for Vue 2

Home Page:https://composition-api.vuejs.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

About expose

LancerComet opened this issue · comments

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?

Stale issue message

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

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