如何在VUE中使用JSX?
是的,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 中非主流,适用于复杂渲染逻辑场景,完整支持需结合构建工具配置。
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.

✅ 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:

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.

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 ofclassName
, andonClick
instead ofonclick
— 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
forv-model
-like behavior.- ✅ Install
@vitejs/plugin-vue-jsx
(for Vite) or Babel preset (for Vue CLI) - ✅ Use
defineComponent
andsetup
to return a render function - ✅ Write JSX with Vue's event and binding conventions
- ✅ Works with TypeScript via
.tsx
Summary
You can use JSX in Vue with the right setup:
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中文网其他相关文章!

热AI工具

Undress AI Tool
免费脱衣服图片

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

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

Stock Market GPT
人工智能驱动投资研究,做出更明智的决策

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

答案:在Vue.js中可通过VueRouter的meta字段和导航守卫动态设置页面标题。1.为每个路由定义meta.title,在router.afterEach中更新document.title;2.若标题依赖异步数据,可在组件内获取后更新;3.Vue3可创建usePageTitle组合式函数复用逻辑;4.对SEO敏感场景应使用@vueuse/head等库支持SSR。

在VueRouter中使用路由参数的核心是通过动态片段捕获URL值。1.定义带参数的路由时,在路径中使用冒号:表示动态参数,如/user/:id;2.在组件中可通过$route.params获取参数,Vue3中可用useRoute;3.可选参数加?,通配符用*捕获未匹配路径;4.跳转可使用router-link或编程式导航并显式传递params。

在Vue3中,原生DOM事件默认直接绑定到组件根元素,无需.native修饰符;若组件为单根元素,可直接使用@event监听,如@click;对于多根节点或需显式控制时,应通过$emit或defineEmits定义并触发自定义事件,以保持跨版本兼容性与清晰的事件接口。

Placestaticassetslikeimagesandfontsinthepublicdirectoryfordirectaccessorinsrc/assetsforbundledprocessing.2.ImportimagesincomponentsusingscriptsetupforautomaticURLresolution.3.Definecustomfontsvia@font-faceinCSSwithaliasedpaths,ensuringViteresolvesthe

在Vue2中,直接赋值无法触发响应式更新,而Vue.set或this.$set能确保新增属性或数组项被正确侦测并更新视图;2.Vue3使用Proxy实现了全面的响应式监听,支持动态添加属性和数组索引修改,因此直接赋值即可触发更新,无需使用$set;3.尽管$set在Vue3中仍存在以兼容旧代码,但已被弃用,推荐优先使用直接赋值或替换整个对象/数组的方式以保证响应性,该方案在两个版本中均有效。

在Vue3的CompositionAPI中,生命周期钩子通过onX函数使用,答案是:1.导入onMounted、onUpdated、onUnmounted等函数并在setup()中调用;2.setup()替代created,无需单独定义;3.可在组合式函数中封装生命周期逻辑以实现复用;4.钩子必须同步调用且可多次注册;5.常见用途包括挂载时获取数据和卸载时清理资源,从而提升代码组织性和可维护性。

在Vue3中创建自定义v-model需定义modelValue属性并emitupdate:modelValue事件;2.可通过v-model:title指定自定义prop名称;3.Vue2默认使用value和input事件,但可通过model选项改为modelValue和update:modelValue以兼容Vue3;4.始终在Vue3中声明emits以确保清晰性和验证;5.避免直接修改prop,应通过事件触发更新,从而使组件像原生输入一样支持双向绑定。

使用VueCLI或Vite均可快速搭建Vue.js项目。2.VueCLI基于webpack,功能丰富,适合需要深度插件集成的项目。3.Vite启动更快,支持热更新,推荐用于新项目。4.多数新项目选择Vite,因其性能优越且配置简洁。
