目录
✅ Why Use JSX in Vue?
For Vite Projects
For Vue CLI Projects
2. Write JSX in Components
3. Using JSX in .vue Files (Optional)
4. JSX with TypeScript (.tsx)
⚠️ Things to Keep in Mind
Summary
首页 web前端 Vue.js 如何在VUE中使用JSX?

如何在VUE中使用JSX?

Jul 31, 2025 am 09:49 AM

是的,Vue 支持 JSX,但需要额外配置,1. 对于 Vite 项目,安装 @vitejs/plugin-vue-jsx 并在 vite.config.js 中添加 vueJsx() 插件;2. 对于 Vue CLI 项目,安装 @vue/babel-preset-jsx 并在 babel.config.js 中添加预设;3. 使用 defineComponent 和 setup 函数返回 JSX 内容,注意使用 class 和 onClick 等 Vue 约定;4. 在 .vue 文件中可通过 h 函数或组件 is 绑定调用 JSX 渲染函数;5. 使用 TypeScript 时,文件命名为 .tsx,配置 tsconfig.json 的 jsx 为 preserve 且 jsxImportSource 为 vue;需注意模板语法如 v-if、v-for 不适用于 JSX,应使用 JavaScript 表达式替代,且 JSX 在 Vue 中非主流,适用于复杂渲染逻辑场景,完整支持需结合构建工具配置。

How to use JSX in Vue?

Vue doesn't support JSX out of the box, but you can use JSX in Vue — especially if you're working with Vue 3 and using the Vite or Vue CLI build setup. Here's how to enable and use JSX in your Vue project.

How to use JSX in Vue?

✅ Why Use JSX in Vue?

While Vue traditionally uses templates, JSX gives you more programmatic control over rendering — useful for complex component logic, dynamic children, or if you're coming from a React background.


1. Enable JSX Support

For Vite Projects

Install the JSX plugin for Vue:

How to use JSX in Vue?
npm install @vitejs/plugin-vue-jsx -D

Then, update your vite.config.js:

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'

export default defineConfig({
  plugins: [
    vue(),
    vueJsx()
  ]
})

Now you can use .jsx or .tsx files, or even write JSX inside .vue files in the setup function.

How to use JSX in Vue?

For Vue CLI Projects

If you're using Vue CLI, install the Babel plugin:

npm install @vue/babel-preset-jsx -D

Then in your babel.config.js:

module.exports = {
  presets: ['@vue/babel-preset-jsx']
}

2. Write JSX in Components

You can now write components using JSX. Here's an example of a .jsx file:

// MyButton.jsx
import { defineComponent } from 'vue'

export default defineComponent({
  props: {
    label: String,
    onClick: Function
  },
  setup(props) {
    return () => (
      <button onClick={props.onClick} class="my-button">
        {props.label}
      </button>
    )
  }
})

Note: Use class instead of className, and onClick instead of onclick — it follows Vue's DOM conventions.


3. Using JSX in .vue Files (Optional)

You can mix JSX in the <script setup> section:

<script setup>
import { h } from 'vue'

const renderDynamic = () => {
  return h('div', { class: 'dynamic' }, [
    h('span', 'Hello JSX inside Vue!')
  ])
}
</script>

<template>
  <div>
    <!-- You can't use JSX in <template> -->
    <!-- But you can call render functions -->
    <component :is="renderDynamic" />
  </div>
</template>

Alternatively, use JSX only in setup() return if you're not using <template>.


4. JSX with TypeScript (.tsx)

If you're using TypeScript, name your file .tsx and define props with interfaces:

import { defineComponent } from 'vue'

interface Props {
  name: string
  count?: number
}

export default defineComponent({
  props: {
    name: { type: String, required: true },
    count: { type: Number, default: 0 }
  },
  setup(props: Props) {
    return () => (
      <div>
        <h1>Hello, {props.name}!</h1>
        <p>Count: {props.count}</p>
      </div>
    )
  }
})

Make sure your tsconfig.json allows JSX:

{
  "compilerOptions": {
    "jsx": "preserve",
    "jsxImportSource": "vue"
  }
}

⚠️ Things to Keep in Mind

  • Templates are still the standard in Vue — JSX is optional and less common.

  • No template features in JSX: directives like v-if, v-for don't work. Use JavaScript expressions instead:

    // Instead of v-if
    {show && <p>Shown</p>}
    
    // Instead of v-for
    {items.map(item => <div key={item.id}>{item.label}</div>)}
  • Event handlers: Use onClick, onInput, etc. (camelCase), and pass functions directly.

  • Use onUpdate:xxx for v-model-like behavior.


  • Summary

    You can use JSX in Vue with the right setup:

    • ✅ Install @vitejs/plugin-vue-jsx (for Vite) or Babel preset (for Vue CLI)
    • ✅ Use defineComponent and setup to return a render function
    • ✅ Write JSX with Vue's event and binding conventions
    • ✅ Works with TypeScript via .tsx

    It's not as common as in React, but it's powerful for dynamic or reusable render logic.

    Basically, Vue supports JSX — just not in templates. Use it where it adds value.

    以上是如何在VUE中使用JSX?的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Laravel 教程
1601
29
PHP教程
1503
276
如何使用VUE构建组件库? 如何使用VUE构建组件库? Jul 10, 2025 pm 12:14 PM

搭建Vue组件库需围绕业务场景设计结构,并遵循开发、测试、发布的完整流程。1.结构设计应按功能模块分类,包括基础组件、布局组件和业务组件;2.使用SCSS或CSS变量统一主题与样式;3.统一命名规范并引入ESLint和Prettier保证代码风格一致;4.配套文档站点展示组件用法;5.使用Vite等工具打包为NPM包并配置rollupOptions;6.发布时遵循semver规范管理版本与changelog。

VUE 2和VUE 3之间的关键差异? VUE 2和VUE 3之间的关键差异? Jul 09, 2025 am 01:29 AM

Vue3相较于Vue2在多个关键方面进行了改进。1.CompositionAPI提供更灵活的逻辑组织方式,允许将相关逻辑集中管理,同时仍支持Vue2的OptionsAPI;2.性能更优且包体积更小,核心库缩小约30%,渲染速度更快并支持更好的摇树优化;3.响应式系统改用ES6Proxy,解决了Vue2中无法自动追踪属性增删的问题,使响应式机制更自然一致;4.内置更好支持TypeScript、支持多根节点片段及自定义渲染器API,提升了灵活性和未来适应性。总体而言,Vue3是对Vue2的平滑升级,

Vue成品资源网站免费入口 完整Vue成品永久在线观看 Vue成品资源网站免费入口 完整Vue成品永久在线观看 Jul 23, 2025 pm 12:39 PM

本文为Vue开发者和学习者精选了一系列顶级的成品资源网站。通过这些平台,你可以免费在线浏览、学习甚至复用海量高质量的Vue完整项目,从而快速提升开发技能和项目实践能力。

如何构建生产的VUE应用程序? 如何构建生产的VUE应用程序? Jul 09, 2025 am 01:42 AM

部署Vue应用到生产环境需优化性能、确保稳定性并提升加载速度。1.使用VueCLI或Vite构建生产版本,生成dist目录并设置正确环境变量;2.若使用VueRouter的history模式,需配置服务器回退至index.html;3.将dist目录部署至Nginx/Apache、Netlify/Vercel或结合CDN加速;4.启用Gzip压缩与浏览器缓存策略以优化加载;5.实施懒加载组件、按需引入UI库、开启HTTPS、防止XSS攻击、添加CSP头及限制第三方SDK域名白名单以增强安全性。

V-Bind指令的目的是什么? V-Bind指令的目的是什么? Jul 10, 2025 pm 12:47 PM

v-bind在Vue.js中用于动态绑定一个或多个属性或组件prop到表达式,它通过保持DOM属性与Vue实例数据同步实现动态更新。常见用法包括绑定src、href、class和style属性,例如使用实现图片源的动态更新;通过:class="{active:isActive}"动态切换类;利用:style="{color:textColor}"设置内联样式;还可传递对象以同时绑定多个属性,如v-bind="{id:myId}";当值需

什么是Vue生命周期钩?命名一些并解释其用例。 什么是Vue生命周期钩?命名一些并解释其用例。 Jul 24, 2025 am 12:08 AM

Vue组件的生命周期钩子用于在特定阶段执行代码。1.created:组件创建后立即调用,适合初始化数据;2.mounted:组件挂载到DOM后调用,适合操作DOM或加载外部资源;3.updated:数据更新导致组件重新渲染时调用,适合响应数据变化;4.beforeUnmount:组件卸载前调用,适合清理事件监听或定时器以防止内存泄漏。这些钩子帮助开发者精准控制组件行为并优化性能。

如何访问组成API中的商店实例? 如何访问组成API中的商店实例? Jul 10, 2025 pm 12:49 PM

在Vue3CompositionAPI中访问store的主要方式是通过useStore()或直接调用Pinia定义的store。1.使用Vuex4时,需导入useStore并在setup()中调用,获取store实例后可访问state、dispatch等;2.使用Pinia时更简洁,定义store后直接导入并调用即可,无需useStore;3.在setup()外部访问store,Vuex需导出store实例后引用,Pinia则可在任意地方调用useXxxStore();4.常见问题包括检查sto

VUE 3中的传送是什么? VUE 3中的传送是什么? Jul 10, 2025 pm 12:50 PM

TeleportinVue3allowsrenderingacomponent'sHTMLinadifferentpartoftheDOMwhilemaintainingnormaldataflow.1.Itsolvesissueslikemodals,tooltips,anddropdownsbeingclippedorhavingz-indexproblemsbymovingthemtoaspecifiedlocationsuchasbodyoraspecificelementlike#mo

See all articles