'How can I use tsx in Vue Compostion setup function?

When I write tsx in @vue/composition-api setup(){}, just like

<script lang="tsx">
import { defineComponent,} from '@vue/composition-api';

export defineComponent({
  setup() {
    const foo = {
      bar: 1
      baz: render() {return (<div>fooo</div>)}
    }
    
    return  { foo }
  }

})

</scirpt>

Then it warns me that 'Vue warn]: Error in data(): "TypeError: Cannot read properties of undefined (reading '$createElement')"'

I see the compiled code there is a ' const h = this.$createElement' inject the 'setup' function. just like

setup(){
 const h = this.$createElement
 const foo = {
    bar: 1
    baz: render() {return h('div', 'fooo')}
  }
}

Because there is no 'this' in setup function, the error occurs.

But How can I fix this problem. I know that remove the tsx outside of the setup function wll works. But I don't want to do that.

(I use vue-cli)



Solution 1:[1]

I find the answer. I shuold use pugin '@vue/babel-preset-jsx', and set 'compositionAPI: true'.

https://www.npmjs.com/package/@vue/babel-preset-jsx

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jeff Wong