3

Is it possible to create a composable function that would use render function so it can display something?

Example:

import { h } from 'vue'

export function useErrorHandling() {
  return {
    render() {
        return h('div', { class: 'bar', innerHTML: 'world!' })      
    }
  }
}
<script setup>
import { useErrorHandling } from './mouse.js'

 useErrorHandling()
</script>

<template>
 hello
</template>

plaground with above example

2 Answers 2

6

Yes It is possible to do that you need just store the value returned by the composable in a variable and use it as a component

const err =  useErrorHandling()
//in template 
// <err />

Playground Example

Sign up to request clarification or add additional context in comments.

2 Comments

Aha, thanks. It's not possible to show something (for example teleported to body toast) without the component instance?
Yes, it is possible. You need to get an instance of the component and append the toast component to it. You can do that by making a reference to the root component using ref or using getCurrentInstance from the internal API. Here is a demo, but I'm not sure if it's a good practice or not 😅
0

createApp and mount can help you.

function HelloWorld({ fontSize }) {
  return h(
    'div',
    {
      style: {
        color: 'red',
        fontSize,
      },
    },
    'Hello World'
  )
}
const app2 = createApp(HelloWorld, {
  fontSize: '30px',
})
app2.mount('#app-2') // pass selector you want to mount

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.