Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
566 views
in Technique[技术] by (71.8m points)

vuejs3 - How do I use jsx syntax in vue files in vue3

For example, I want to use these codes in Hello.vue:

<template>
  <RenderComponent :data="hello" />
</template>

<script>
import { ref, computed, defineComponent } from 'vue'

export default defineComponent({
  name: 'CustomComponent',

  setup() {
    const name = ref('Lily')

    const hello = computed(() => {
      return <span>Hello {name}!</span>;
    }) 
    return {
       hello
    }
  }
})
</script>

But IDE will throw error like "Failed to parse source for import analysis because the content contains invalid JS syntax."

I have already install @vitejs/plugin-vue-jsx and @vue/babel-plugin-jsx, and I can use *.jsx files successfully.

What should I do?

question from:https://stackoverflow.com/questions/65643112/how-do-i-use-jsx-syntax-in-vue-files-in-vue3

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should use jsx inside file named Hello.jsx without using template and script tags, and don't return a jsx from a property and return a function ()=> to render the jsx, the following example should work :

import { ref, computed, defineComponent } from 'vue'

export default defineComponent({
  name: 'CustomComponent',

  setup() {
    const name = ref('Lily')

    const hello = computed(() => {
      return `Hello ${name.value}!`;
    }) 
    return ()=><RenderComponent data={hello} />
  }
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...