Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

JSX for Vue 3

License

NotificationsYou must be signed in to change notification settings

vuejs/babel-plugin-jsx

Repository files navigation

npm packageissues-helper

To add Vue JSX support.

English |简体中文

Installation

Install the plugin with:

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

Then add the plugin to your babel config:

{"plugins": ["@vue/babel-plugin-jsx"]}

Usage

options

transformOn

Type:boolean

Default:false

transformon: { click: xx } toonClick: xxx

optimize

Type:boolean

Default:false

When enabled, this plugin generates optimized runtime code usingPatchFlags andSlotFlags to improve rendering performance. However, due to JSX's dynamic nature, the optimizations are not as comprehensive as those in Vue's official template compiler.

Since the optimized code may skip certain re-renders to improve performance, we strongly recommend thorough testing of your application after enabling this option to ensure everything works as expected.

isCustomElement

Type:(tag: string) => boolean

Default:undefined

configuring custom elements

mergeProps

Type:boolean

Default:true

merge static and dynamic class / style attributes / onXXX handlers

enableObjectSlots

Type:boolean

Default:true

Whether to enableobject slots (mentioned below the document) syntax". It might be useful in JSX, but it will add a lot of_isSlot condition expressions which increase your bundle size. Andv-slots is still available even ifenableObjectSlots is turned off.

pragma

Type:string

Default:createVNode

Replace the function used when compiling JSX expressions.

resolveType

Type:boolean

Default:false

(Experimental) Infer component metadata from types (e.g.props,emits,name). This is an experimental feature and may not work in all cases.

Syntax

Content

functional component

constApp=()=><div>Vue 3.0</div>;

with render

constApp={render(){return<div>Vue 3.0</div>;},};
import{withModifiers,defineComponent}from'vue';constApp=defineComponent({setup(){constcount=ref(0);constinc=()=>{count.value++;};return()=>(<divonClick={withModifiers(inc,['self'])}>{count.value}</div>);},});

Fragment

constApp=()=>(<><span>I'm</span><span>Fragment</span></>);

Attributes / Props

constApp=()=><inputtype="email"/>;

with a dynamic binding:

constplaceholderText='email';constApp=()=><inputtype="email"placeholder={placeholderText}/>;

Directives

v-show

constApp={data(){return{visible:true};},render(){return<inputv-show={this.visible}/>;},};

v-model

Note: You should pass the second param as string for usingarg.

<inputv-model={val}/>
<inputv-model:argument={val}/>
<inputv-model={[val,['modifier']]}/>// Or<inputv-model_modifier={val}/>
<Av-model={[val,'argument',['modifier']]}/>// Or<inputv-model:argument_modifier={val}/>

Will compile to:

h(A,{argument:val,argumentModifiers:{modifier:true,},'onUpdate:argument':($event)=>(val=$event),});

v-models (Not recommended since v1.1.0)

Note: You should pass a Two-dimensional Arrays to v-models.

<Av-models={[[foo],[bar,'bar']]}/>
<Av-models={[[foo,'foo'],[bar,'bar'],]}/>
<Av-models={[[foo,['modifier']],[bar,'bar',['modifier']],]}/>

Will compile to:

h(A,{modelValue:foo,modelModifiers:{modifier:true,},'onUpdate:modelValue':($event)=>(foo=$event),bar:bar,barModifiers:{modifier:true,},'onUpdate:bar':($event)=>(bar=$event),});

custom directive

Recommended when using string arguments

constApp={directives:{custom:customDirective},setup(){return()=><av-custom:arg={val}/>;},};
constApp={directives:{custom:customDirective},setup(){return()=><av-custom={[val,'arg',['a','b']]}/>;},};

Slot

Note: Injsx,v-slot should be replaced withv-slots

constA=(props,{ slots})=>(<><h1>{slots.default ?slots.default() :'foo'}</h1><h2>{slots.bar?.()}</h2></>);constApp={setup(){constslots={bar:()=><span>B</span>,};return()=>(<Av-slots={slots}><div>A</div></A>);},};// orconstApp={setup(){constslots={default:()=><div>A</div>,bar:()=><span>B</span>,};return()=><Av-slots={slots}/>;},};// or you can use object slots when `enableObjectSlots` is not false.constApp={setup(){return()=>(<><A>{{default:()=><div>A</div>,bar:()=><span>B</span>,}}</A><B>{()=>'foo'}</B></>);},};

In TypeScript

tsconfig.json:

{"compilerOptions": {"jsx":"preserve"  }}

Who is using


Ant Design Vue

Vant

Element Plus

Vue Json Pretty

Compatibility

This repo is only compatible with:

  • Babel 7+
  • Vue 3+

[8]ページ先頭

©2009-2025 Movatter.jp