Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
765 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 Reply

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} />
  }
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...